0

What is the best way to update the slug of a custom taxonomy that is registered by a plugin? I want to know the minimal amount of parameters for this or if i need to go and copy paste all of the original parameters. This seems like it can be error prone if the plugin has an update. The codex says you will overwrite the original when using register_taxonomy so is there a better way to do this or a different function?

2

register_taxonomy triggers the action registered_taxonomy immediately after it's registered, which gives you the arguments it was registered with. As long as the taxonomy key doesn't change, you can hook that action, modify the arguments, then re-register it.

function wpd_update_taxonomy_args( $taxonomy, $object_type, $args ){
    if( 'plugin_tax' == $taxonomy ){

        // remove this action so we don't create an infinite loop!
        remove_action( current_action(), __FUNCTION__ );

        // change arguments
        $args['rewrite'] = array( 'slug' => 'foobar' );

        // re-register
        register_taxonomy( $taxonomy, $object_type, $args );
    }
}
add_action( 'registered_taxonomy', 'wpd_update_taxonomy_args', 10, 3 );
  • Ok since the plugin is already installed and has created the taxonomy, I noticed I can't get this logic to execute? – Shawn Feb 16 '15 at 18:54
  • taxonomies (and post types) are registered on every request, it doesn't matter that the taxonomy was previously registered. make sure you flush rewrite rules after changing any rewrites. rewrite rules are the only part of taxonomies and post types that persist beyond a single request, because generating them is an expensive process. you flush rules quickly by visiting the permalinks settings page. – Milo Feb 16 '15 at 20:13
  • Ok it was a typo on my part sorry. As an aside will this method also keep all the other settings the exact same? – Shawn Feb 16 '15 at 20:28
  • yes, all settings will remain the same, as the action is receiving the settings directly from the same variable it just registered the taxonomy with. – Milo Feb 16 '15 at 20:29
  • Ok that is good to know. I wasn't sure since we are firing off another register_taxonomy after removing the current action. – Shawn Feb 16 '15 at 20:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.