Add Snippet To Project
Are you wanting to remove default taxonomies, such as the category option, from your WordPress site? While WordPress provides many built-in categories and taxonomies, sometimes they might not fit with your website’s goals or structure. Thankfully, WordPress provides flexibility here as well, allowing you to remove or edit these as you see fit. This article will guide you step-by-step on how to remove the default taxonomies from your WordPress website.
function wpturbo_remove_default_taxonomies() {
global $wp_taxonomies;
unregister_taxonomy_for_object_type('category', 'post');
}
add_action( 'init', 'wpturbo_remove_default_taxonomies' );
The code snippet begins by defining a function called wpturbo_remove_default_taxonomies(). This function’s purpose is to remove the default ‘category’ taxonomy from posts in WordPress.
Inside this function, there’s a call to the unregister_taxonomy_for_object_type() function. This is a built-in WordPress function responsible for disconnecting a taxonomy from a specified post type. Here’s what’s happening in this call to unregister_taxonomy_for_object_type():
unregister_taxonomy_for_object_type('category', 'post');
The function takes two parameters:
-
The first parameter is the taxonomy that you want to unregister. In our case, this is ‘category’, which corresponds to the default category taxonomy in a standard WordPress installation.
-
The second parameter is the object type that the taxonomy is to be unregistered from. For our code, it’s ‘post’, which corresponds to the default post type in WordPress.
Essentially, the unregister_taxonomy_for_object_type() function line in our code is telling WordPress to disconnect the ‘category’ taxonomy from ‘post’ type.
The last step hooks the wpturbo_remove_default_taxonomies() function to the ‘init’ action hook in WordPress:
add_action( 'init', 'wpturbo_remove_default_taxonomies' );
The ‘init’ action is triggered after WordPress has finished loading, but before any headers are sent. By using the ‘init’ hook, we ensure that our wpturbo_remove_default_taxonomies() is loaded and executed each time WordPress runs, which effectively disconnects the ‘category’ taxonomy from ‘post’ type.
This means that after implementing the code, when you navigate to ‘Add New Post’ or ‘Edit Post’, you will no longer see the ‘Categories’ box.
