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?
1 Answer
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?– ShawnFeb 16, 2015 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.– MiloFeb 16, 2015 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?– ShawnFeb 16, 2015 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.– MiloFeb 16, 2015 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.– ShawnFeb 16, 2015 at 20:34