Using WordPress ‘register_{$taxonomy}_taxonomy_args’ PHP filter

The “register_{$taxonomy}_taxonomy_args” dynamic filter allows you to modify the arguments for registering a specific taxonomy in WordPress.

Usage

$args = apply_filters("register_{$taxonomy}_taxonomy_args", $args, $taxonomy, $object_type);

Parameters

  • $args (array): Array of arguments for registering a taxonomy. See the register_taxonomy() function for accepted arguments.
  • $taxonomy (string): Taxonomy key.
  • $object_type (string[]): Array of names of object types for the taxonomy.

Examples

Change the default capabilities for a custom taxonomy

Scenario: You want to change the default capabilities for a custom taxonomy called ‘location’.

add_filter('register_location_taxonomy_args', 'modify_location_capabilities', 10, 2);

function modify_location_capabilities($args) {
    $args['capabilities'] = array(
        'manage_terms' => 'manage_locations',
        'edit_terms'   => 'edit_locations',
        'delete_terms' => 'delete_locations',
        'assign_terms' => 'assign_locations',
    );

    return $args;
}

// This code modifies the default capabilities for the 'location' taxonomy by using a custom filter.

Disable the REST API for a custom taxonomy

Scenario: You want to disable the REST API for a custom taxonomy called ‘genre’.

add_filter('register_genre_taxonomy_args', 'disable_genre_rest_api', 10, 2);

function disable_genre_rest_api($args) {
    $args['show_in_rest'] = false;

    return $args;
}

// This code disables the REST API for the 'genre' taxonomy by setting 'show_in_rest' to false.

Set a custom meta box for a custom taxonomy

Scenario: You want to set a custom meta box for a custom taxonomy called ‘skill’.

add_filter('register_skill_taxonomy_args', 'set_skill_custom_meta_box', 10, 2);

function set_skill_custom_meta_box($args) {
    $args['meta_box_cb'] = 'custom_skill_meta_box';

    return $args;
}

function custom_skill_meta_box() {
    // Your custom meta box code here.
}

// This code sets a custom meta box for the 'skill' taxonomy by providing a custom callback function.

Customize rewrite rules for a custom taxonomy

Scenario: You want to customize the rewrite rules for a custom taxonomy called ‘department’.

add_filter('register_department_taxonomy_args', 'customize_department_rewrite_rules', 10, 2);

function customize_department_rewrite_rules($args) {
    $args['rewrite'] = array(
        'slug'         => 'team',
        'with_front'   => false,
        'hierarchical' => true,
    );

    return $args;
}

// This code customizes the rewrite rules for the 'department' taxonomy by providing an array of new rules.

Change the default term for a custom taxonomy

Scenario: You want to change the default term for a custom taxonomy called ‘color’.

add_filter(‘register_color_taxonomy_args’, ‘change_color_default_term’, 10, 2);

function change_color_default_term($args) {
$args[‘default_term’] = array(
‘name’ => ‘Blue’,
‘slug’ => ‘blue’,
‘description’ => ‘The default color is blue.’,
);

return $args;
}

// This code changes the default term for the ‘color’ taxonomy by providing an array of new default term properties