The create_initial_taxonomies() WordPress PHP function is used to create the initial taxonomies. It is fired twice, once in wp-settings.php before plugins are loaded (for backward compatibility reasons), and again on the ‘init’ action. It is crucial to avoid registering rewrite rules before the ‘init’ action.
Table of contents
Usage
Here’s a basic example of how to use the function:
add_action( 'init', 'create_initial_taxonomies', 0 );
Parameters
The create_initial_taxonomies() function does not have any parameters.
More Information
See WordPress Developer Resources: create_initial_taxonomies
This function is integral to WordPress and there’s no sign of deprecation. It is typically not used by developers directly as it’s more of a core function that WordPress uses internally.
Examples
Check if Taxonomies are Created
if ( taxonomy_exists('category') && taxonomy_exists('post_tag') && taxonomy_exists('nav_menu') && taxonomy_exists('link_category') ) {
echo "Initial taxonomies created!";
}
In this example, we are checking if the initial taxonomies, like ‘category’, ‘post_tag’, ‘nav_menu’, and ‘link_category’, exist. If they do, it prints “Initial taxonomies created!”.
Print All Taxonomies
global $wp_taxonomies; print_r(array_keys($wp_taxonomies));
Here, we are printing all the registered taxonomies after the initial taxonomies are created.
Count Number of Taxonomies
global $wp_taxonomies; echo count($wp_taxonomies);
In this code snippet, we are counting the number of registered taxonomies.
Remove a Taxonomy
global $wp_taxonomies; unset($wp_taxonomies['post_tag']);
In this example, we are removing a taxonomy ‘post_tag’ from the list of registered taxonomies.
Add a New Taxonomy
add_action( 'init', 'create_my_taxonomy', 0 );
function create_my_taxonomy() {
register_taxonomy( 'my_taxonomy', 'post', array(
'label' => __( 'My Taxonomy' ),
'rewrite' => array( 'slug' => 'my_taxonomy' ),
'hierarchical' => true,
) );
}
Here, we are creating a new taxonomy ‘my_taxonomy’ after the initial taxonomies are created. We are using ‘init’ action hook to ensure it is added after WordPress has created its initial taxonomies.