[wp-hackers] Setting custom taxonomy on the fly

Dougal Campbell dougal at gunters.org
Sun Apr 17 17:49:00 UTC 2011


On Apr 17 2011 1:41 PM, Otto wrote:
> On Sun, Apr 17, 2011 at 11:49 AM, Dougal Campbell<dougal at gunters.org>  wrote:
>> So that leaves us with just a couple of other opportunities to act: the
>> 'wp_insert_post_data' filter, and the 'wp_insert_post' action. With the
>> filter, I can detect my sentinel string and strip it back out of the post,
>> but I can't set the post_format, because we don't have a post_ID yet. But if
>> we get all the way to the 'wp_insert_post' action, updating the post content
>> to strip out the sentinel is a problem, because using wp_update_post() will
>> in turn call wp_insert_post(), which is a recursive loop.
> Why not just use both?
>
> add_filter('wp_insert_post_data','strip_content');
> function strip_content($data) {
>    global $my_format;
>    // detect format in $data['post_content'] and modify it
>    // set $my_format = detected-format;
>    return $data;
> }
>
> add_action('wp_insert_post','add_format');
> function add_format($post_ID) {
>    global $my_format;
>    set_post_format($post_ID, $my_format);
> }
>

That's the work-around I came up with. It's just uglier than it needs to 
be. An appropriately placed filter would make this much easier for 
developers in the future. And if it was a filter on the arguments before 
the extract() in wp_insert_post(), it would be generic enough to handle 
arbitrary custom taxonomies (not just post formats), just by adding the 
'tax_input' key to the array, with appropriate term data.

-- 
Dougal Campbell <dougal at gunters.org>
http://dougal.gunters.org/
http://twitter.com/dougal
http://twitual.com/


More information about the wp-hackers mailing list