Today I needed to change the arguments on a custom taxonomy that was already registered by a third party plugin. Specifically I wanted to set the show_admin_column argument to true and change the rewrite slug so that it wasn’t just the taxonomy slug. In this case, it was a “People” post type with a “People Category” custom taxonomy.
I was surprised this wasn’t asked before, so here’s a question and answer.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
register_taxonomy() is the tool for the job. From the Codex:
This function adds or overwrites a taxonomy.
One option would be to copy the register_taxonomy() $args and modify them. However, that would mean that any future changes to the original register_taxonomy() code would be overwritten.
Therefore, at least in this case, it’s preferable to get the original arguments, modify the ones I want to change, and then re-register the taxonomy. Inspiration for this solution goes to @Otto in this answer to a similar question about custom post types.
Using the people custom post type and people_category taxonomy from the example, this’ll do it:
function wpse_modify_taxonomy() {
// get the arguments of the already-registered taxonomy
$people_category_args = get_taxonomy( 'people_category' ); // returns an object
// make changes to the args
// in this example there are three changes
// again, note that it's an object
$people_category_args->show_admin_column = true;
$people_category_args->rewrite['slug'] = 'people';
$people_category_args->rewrite['with_front'] = false;
// re-register the taxonomy
register_taxonomy( 'people_category', 'people', (array) $people_category_args );
}
// hook it up to 11 so that it overrides the original register_taxonomy function
add_action( 'init', 'wpse_modify_taxonomy', 11 );
Note above that I typecast the third register_taxonomy() argument to the expected array type. This isn’t strictly necessary as register_taxonomy() uses wp_parse_args() which can handle an object or array. That said, register_taxonomy()‘s $args are supposed to be submitted as an array according to the Codex, so this feels right to me.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0