Adding Custom Taxonomies to WordPress Plugin
Posted on 16th June 2023
WordPress plugins are great for adding custom functionality to a WordPress site. In this article, we will show you how to add custom taxonomies to a WordPress plugin.
First, you need to create a new file in your plugin directory and name it taxonomies.php. Next, you will need to paste the following code into your newly created taxonomies.php file:
In the code above, we have created a new plugin called Add Custom Taxonomies. This plugin will add custom taxonomies to your WordPress site.
Next, you need to replace the /* Add your custom taxonomies here */ comment with the following code:
register_taxonomy( ‘taxonomy_name’, ‘post_type’, array(
‘hierarchical’ => true,
‘label’ => ‘Taxonomy Name’,
‘show_ui’ => true,
‘show_admin_column’ => true,
‘query_var’ => true,
‘rewrite’ => array( ‘slug’ => ‘taxonomy-name’ ),
) );
In the code above, we have registered a new taxonomy called Taxonomy Name. This taxonomy is hierarchical and will be displayed in the WordPress admin area.
Now that you have added the code to register your custom taxonomy, you need to activate your plugin. You can do this by going to the Plugins page of your WordPress admin area and clicking on the Activate link under the Add Custom Taxonomies plugin.
Once your plugin is activated, you will see your new taxonomy listed in the left sidebar of your WordPress admin area. You can now start assigning terms to your posts.
Adding Custom Taxonomies to WordPress Plugin
In this article, we will learn how to add custom taxonomies to a WordPress plugin. Custom taxonomies are a great way to organize your content and make it easier for your users to find what they’re looking for.
First, we will need to register our custom taxonomy. We can do this by hooking into the init action and using the register_taxonomy function.
array(
‘name’ => __( ‘My Plugin Taxonomies’ ),
‘singular_name’ => __( ‘My Plugin Taxonomy’ )
),
‘hierarchical’ => true,
)
);
}
?>
In the code above, we are registering a custom taxonomy called “my_plugin_taxonomy” for our custom post type “my_plugin”. We are also setting the custom taxonomy to be hierarchical, which means that it can have parent and child terms.
Next, we need to register our taxonomy with our plugin. We can do this by hooking into the my_plugin_init action and using the register_taxonomy_for_object_type function.
Now that our taxonomy is registered, we can start adding terms to our custom post type. We can do this by going to the WordPress admin area and navigating to My Plugin -> My Plugin Taxonomies. From here, we can add new terms and assign them to our custom post type.
We can also add terms to our custom post type programmatically. We can do this by hooking into thesave_post action and using the wp_insert_term function.
cap->edit_post, $post_id ) )
return $post_id;
// Get the post terms
$terms = get_terms( ‘my_plugin_taxonomy’, array(
‘hide_empty’ => false
) );
// Get the post terms
$selected_terms = isset( $_POST[‘my_plugin_taxonomy’] ) ? $_POST[‘my_plugin_taxonomy’] : array();
// Set the post terms
wp_set_object_terms( $post_id, $selected_terms, ‘my_plugin_taxonomy’ );
}
?>
In the code above, we are first checking if the current user has permission to edit the post. We then get an array of all the terms in our custom taxonomy. Next, we get an array of the terms that were selected in the post form. Finally, we use the wp_set_object_terms function to set the terms for our post.
Now that we know how to add custom taxonomies to our WordPress plugin, we can start using them to organize our content.
When it comes to custom taxonomies in WordPress, there are two main approaches you can take. You can either use the built-in custom taxonomy functionality, or you can create your own custom taxonomy plugin.
If you decide to use the built-in custom taxonomy functionality, you can add your custom taxonomies to your plugin by using the register_taxonomy() function. This function allows you to specify the details of your custom taxonomy, such as its name, labels, and capabilities.
Once you have registered your custom taxonomy, you can then add it to any post type using the register_taxonomy_for_object_type() function. This function allows you to specify which post types your custom taxonomy should be associated with.
If you decide to create your own custom taxonomy plugin, you will need to include the following files:
Your plugin file, which should be named {your-plugin-name}.php
A taxonomy file, which should be named taxonomy-{your-taxonomy-name}.php
An archive file, which should be named archive-{your-taxonomy-name}.php
Your plugin file should contain the following code:
<?php
/*
Plugin Name: {Your Plugin Name}
Description: {Your Plugin Description}
*/
// Your plugin code goes here
Your taxonomy file should contain the following code:
<?php
/*
Taxonomy Name: {Your Taxonomy Name}
*/
// Your taxonomy code goes here
Your archive file should contain the following code:
<?php
/*
Template Name: {Your Taxonomy Name} Archive
*/
// Your archive code goes here
Once you have created these files, you will need to upload them to your plugin directory. You can then activate your plugin and your custom taxonomy will be ready to use.