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.

  • Just a side note for anyone getting stuck, remember to flush the permalinks before checking the result. – un.pez.vivo Dec 10 '17 at 12:30
up vote 16 down vote accepted

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.

  • I'm trying to do this in order to change a flat taxonomy to a hierarchical taxonomy. I'm simply changing the hierarchical property to true. By doing this, it causes two meta boxes for the taxonomy on the right of the post edit screen, and I'm unable to actually add taxonomies... – Joel Worsham Apr 8 '15 at 16:48
  • Make sure you use the same slug (e.g. 'people_category') as the original taxonomy so that you overwrite it. – mrwweb Apr 8 '15 at 17:21
  • Genius! Pure genius! Worked perfectly! – DaveyJake May 31 '15 at 10:02
  • 1
    I found an issue with this solution : the capabilities parameter is stored internally as cap so it won't be passed to the new registered taxonomy. See my answer to a similar question here – Fabien Quatravaux Dec 14 '15 at 8:37
  • seems adding flush_rewrite_rules(); after register_taxonomy is necessary for applying rewrite rules of new slug – Sajjad Shirazy Apr 30 at 14:57

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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