23

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.

1
  • Just a side note for anyone getting stuck, remember to flush the permalinks before checking the result. Commented Dec 10, 2017 at 12:30

2 Answers 2

29

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.

9
  • 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... Commented Apr 8, 2015 at 16:48
  • Make sure you use the same slug (e.g. 'people_category') as the original taxonomy so that you overwrite it.
    – mrwweb
    Commented Apr 8, 2015 at 17:21
  • 1
    Genius! Pure genius! Worked perfectly!
    – DaveyJake
    Commented May 31, 2015 at 10:02
  • 3
    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 Commented Dec 14, 2015 at 8:37
  • 1
    This is wonderful, I used it to add hierarchical urls to a custom taxonomy.
    – Brian Peat
    Commented Dec 2, 2018 at 23:36
3
add_filter( 'register_taxonomy_args', function( $args, $taxonomy, $object_type ) {
    if( $taxonomy !== 'people_category' || ! $object_type || $object_type[0] !== 'people' ) return $args;
    $args['show_admin_column'] = true;
    $args['rewrite']['slug'] = 'people';
    return $args;
}, 10, 3);
1
  • 1
    This is the proper way of doing it nowadays. Don't forget to resave the Permalinks in Settings.
    – dev_masta
    Commented Aug 25, 2023 at 1:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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