In my WordPress Plugin, I use the following code to automatically add Meta Boxes for all installed Taxonimies:
add_action( 'init', 'my_plugin_register_taxonomies_for_object_type');
function my_plugin_register_taxonomies_for_object_type()
{
$args = array(
'public' => true,
);
$output = 'objects';
$taxonomies = get_taxonomies( $args, $output );
foreach ( $taxonomies as $taxonomy )
{
register_taxonomy_for_object_type( $taxonomy->name, 'jt_condet_content' );
}
}
This works really well for all Taxonmies, even those installed via plugins. However, I noticed that one Taxonomy is missing. It is a normal Post Taxonomy installed by a Child Theme using register_taxonomy() in the init hook of the Theme.
Any idea why only this Taxonomy is missed by my plugins my_plugin_register_taxonomies_for_object_type()? It drives me crazy. I want to show meta boxes for all installed public Taxonomies, no matter if they are build in, installed via a Plugins or via a Themes.
I played around a little bit with the action hook and wp_loaded seems to work but i'm not 100% confident that this is the right hook to use or am I fine?
Can you explain me the downsided of using wp_loaded over init?
If you're using
initand the child theme is also usinginityou probably just need to change the priority of your hook to ensure it runs after theirs. Try doing:Assuming their theme uses the default (10) yours will run after theirs and their taxonomy should be available.
The only downside I can think for using
wp_loadedis thatwp_loadedwill run after a few other actions such aswidgets_init,register_sidebarand probably a few others. If there are any other callbacks by other plugins / themes that would expect your post type to have these taxonomies registered, and they hook beforewp_loaded, they'd fail becausewp_loadedruns after them. It's likelywp_loadedwould be fine in most cases.