[wp-hackers] How to dynamically register hook with post type ?

Tommy Leunen tom at tommyleunen.com
Thu Jan 19 04:16:08 UTC 2012


Actually, I didn't explain very well my problem. Using hook instead of
action and register instead of add ^^

What I look for is to add action on "publish_X" dynamically according to
each custom types (they would be created by hand using register_post_type
like you said).

So what I did the first time is simply to add this in my php file :
$types =  get_post_types();
foreach($types as $type)
{
add_action('publish_'.$type, 'my_publish_post', 99);
}

But that code doesn't work if my blog is used in a network site
installation.
I then figure out that the following code works, but I'm wondering if it's
really correct to do that with this way :
add_action('init', 'my_init');
function my_init()
{
$types =  get_post_types();
foreach($types as $type)
{
add_action('publish_'.$type, 'my_publish_post', 99);
}
}

Thanks.

2012/1/18 scribu <scribu at gmail.com>

> If those post types are defined using another plugin (and are therefore
> stored somewhere in the options table), then you're out of luck. The post
> types will only be available on the blog where you defined them.
>
> The obvious solution is to register them by hand, using
> register_post_type().
>
> --
> http://scribu.net
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


More information about the wp-hackers mailing list