Using WordPress ‘registered_taxonomy_for_object_type’ PHP action

The registered_taxonomy_for_object_type WordPress PHP action fires after a taxonomy is registered for an object type.

Usage

add_action('registered_taxonomy_for_object_type', 'your_custom_function', 10, 2);

function your_custom_function($taxonomy, $object_type) {
    // your custom code here
}

Parameters

  • $taxonomy (string) – Taxonomy name.
  • $object_type (string) – Name of the object type.

More information

See WordPress Developer Resources: registered_taxonomy_for_object_type

Examples

Display a message after registering a taxonomy for a custom post type

This example will display a message in the admin area after a taxonomy is registered for a custom post type.

add_action('registered_taxonomy_for_object_type', 'display_message_after_registering_taxonomy', 10, 2);

function display_message_after_registering_taxonomy($taxonomy, $object_type) {
    if ('your_custom_post_type' === $object_type) {
        add_action('admin_notices', 'show_success_message');
    }
}

function show_success_message() {
    echo '<div class="notice notice-success is-dismissible">';
    echo '<p>Successfully registered the taxonomy for your custom post type!</p>';
    echo '</div>';
}

Log taxonomy registration

Log the taxonomy and object type when a taxonomy is registered for an object type.

add_action('registered_taxonomy_for_object_type', 'log_taxonomy_registration', 10, 2);

function log_taxonomy_registration($taxonomy, $object_type) {
    error_log("Registered taxonomy '{$taxonomy}' for object type '{$object_type}'");
}

Modify registered taxonomy labels

Modify the labels of a taxonomy after it has been registered for a custom post type.

add_action('registered_taxonomy_for_object_type', 'modify_taxonomy_labels', 10, 2);

function modify_taxonomy_labels($taxonomy, $object_type) {
    if ('your_custom_taxonomy' === $taxonomy) {
        $taxonomy_object = get_taxonomy($taxonomy);
        $taxonomy_object->labels->name = 'New Taxonomy Name';
    }
}

Add a custom capability to a taxonomy

Add a custom capability to a taxonomy after it has been registered for a custom post type.

add_action('registered_taxonomy_for_object_type', 'add_custom_capability_to_taxonomy', 10, 2);

function add_custom_capability_to_taxonomy($taxonomy, $object_type) {
    if ('your_custom_taxonomy' === $taxonomy) {
        $taxonomy_object = get_taxonomy($taxonomy);
        $taxonomy_object->cap->assign_terms = 'assign_custom_terms';
    }
}

Automatically assign a taxonomy term to a custom post type

Automatically assign a taxonomy term to a custom post type when the taxonomy is registered.

add_action('registered_taxonomy_for_object_type', 'auto_assign_taxonomy_term', 10, 2);

function auto_assign_taxonomy_term($taxonomy, $object_type) {
    if ('your_custom_taxonomy' === $taxonomy && 'your_custom_post_type' === $object_type) {
        add_action('save_post_your_custom_post_type', 'assign_default_term', 100, 2);
    }
}

function assign_default_term($post_id, $post) {
    $default_term_id = 1; // Replace with your default term ID
    wp_set_object_terms($post_id, $default_term_id, 'your_custom_taxonomy');
}