Using WordPress ‘registered_taxonomy’ PHP action

The registered_taxonomy WordPress PHP action fires after a taxonomy is registered.

Usage

add_action('registered_taxonomy', 'my_custom_function', 10, 3);
function my_custom_function($taxonomy, $object_type, $args) {
  // Your custom code here
}

Parameters

  • $taxonomy (string): Taxonomy slug.
  • $object_type (array|string): Object type or array of object types.
  • $args (array): Array of taxonomy registration arguments.

More information

See WordPress Developer Resources: registered_taxonomy

Examples

Display a message when a taxonomy is registered

Display a custom message in the WordPress admin when a taxonomy is registered.

add_action('registered_taxonomy', 'show_message_after_taxonomy_registered', 10, 3);
function show_message_after_taxonomy_registered($taxonomy, $object_type, $args) {
  if (is_admin()) {
    echo "The '{$taxonomy}' taxonomy was successfully registered.";
  }
}

Add custom metadata to a registered taxonomy

Add a custom metadata field to the taxonomy term edit screen.

add_action('registered_taxonomy', 'add_custom_metadata_to_taxonomy', 10, 3);
function add_custom_metadata_to_taxonomy($taxonomy, $object_type, $args) {
  add_term_meta($taxonomy, 'custom_meta_key', 'custom_meta_value');
}

Modify taxonomy arguments

Modify the taxonomy arguments after it is registered.

add_action('registered_taxonomy', 'modify_taxonomy_arguments', 10, 3);
function modify_taxonomy_arguments($taxonomy, $object_type, $args) {
  $args['hierarchical'] = true;
  register_taxonomy($taxonomy, $object_type, $args);
}

Log registered taxonomies

Log the registered taxonomies in a text file.

add_action('registered_taxonomy', 'log_registered_taxonomies', 10, 3);
function log_registered_taxonomies($taxonomy, $object_type, $args) {
  $log_file = fopen("registered_taxonomies.log", "a");
  $log_entry = "{$taxonomy} registered for object type(s): " . implode(', ', (array)$object_type) . "\n";
  fwrite($log_file, $log_entry);
  fclose($log_file);
}

Register custom post type for a specific taxonomy

Register a custom post type only for a specific taxonomy after it is registered.

add_action('registered_taxonomy', 'register_cpt_for_specific_taxonomy', 10, 3);
function register_cpt_for_specific_taxonomy($taxonomy, $object_type, $args) {
  if ($taxonomy === 'my_specific_taxonomy') {
    $cpt_args = array(
      'label' => 'Custom Post Type for My Specific Taxonomy',
      'public' => true,
      'taxonomies' => array($taxonomy),
    );
    register_post_type('my_specific_cpt', $cpt_args);
  }
}