WordPress Plugin Register Taxonomy for Object Type of Theme Taxonomies

361 views Asked by At

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?

1

There are 1 answers

1
Jared Cobb On BEST ANSWER

If you're using init and the child theme is also using init you probably just need to change the priority of your hook to ensure it runs after theirs. Try doing:

add_action( 'init', 'my_plugin_register_taxonomies_for_object_type', 20 );

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_loaded is that wp_loaded will run after a few other actions such as widgets_init, register_sidebar and 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 before wp_loaded, they'd fail because wp_loaded runs after them. It's likely wp_loaded would be fine in most cases.