Home >Backend Development >PHP Tutorial >Custom Plugins: A Comprehensive Guide to WordPress Taxonomies for Beginners
In this series, we’ve been looking at WordPress taxonomies: what they are, how they work, how to distinguish the different types that exist, and how they are stored in the underlying database. p>
The only thing we have to do is assemble a plugin that demonstrates how to use the API to implement our own custom taxonomies. So, in this article, we will do exactly that.
Recall the first article in this series:
Classification is one of those words that most people have never heard of or used. Basically, a taxonomy is a way of grouping things together.
Throughout this series, we have been using photography and videography as classification examples. Therefore, for the plugin we are going to build, we will include hierarchical and non-hierarchical taxonomies related to these two classifications.
Finally, the plugin will work with the existing standard post types that come with WordPress. This should provide the most flexibility as it relates to building the plugin, demonstrating the concept, and using it in your own installation.
For the purposes of the example plugin, we'll call it My Custom Taxonomy, and we'll build it in the following stages:
Before doing anything else, create a directory named my-custom-taxonomies
in wp-content/plugins
and introduce a directory named My custom taxonomy.php
.
Add the following code comment block to the file:
<?php /** * My Custom Taxonomies * * Demonstrates how to create custom taxonomies using the WordPress API. * Showcases both hierarchical and non-hierarchical taxonomies. * * @link https://code.tutsplus.com/series/the-beginners-guide-to-wordpress-taxonomies--cms-706 * @since 1.0.0 * @package Custom_Taxonomies * * @wordpress-plugin * Plugin Name: My Custom Taxonomies * Plugin URI: http://example.com/plugin-name-uri/ * Description: Demonstrates how to create custom taxonomies using the WordPress API. * Version: 1.0.0 * Author: Tom McFarlin * Author URI: http://tommcfarlin.com/ * License: GPL-2.0+ * License URI: http://www.gnu.org/licenses/gpl-2.0.txt */
At this point, you should be able to log into your WordPress dashboard, view the name of the plugin, and activate it. Of course, nothing will actually happen because we haven't done anything to the source code yet.
Next, we need to create another file to actually power the plugin. This will be based on object-oriented programming principles, so we will create a file called class-my-custom-taxonomies.php
.
Don't worry about filling it with any source code just yet. Let’s go back to my-custom-taxonomies.php
and add a condition to ensure that the core plugin file cannot be run outside of the WordPress environment.
<?php // If this file is called directly, abort. if ( ! defined( 'WPINC' ) ) { die; }
Place this directly under the code comment we provided above.
At this point, we are ready to write the code that actually drives the plugin. So let's define a class and a basic function for initialization:
<?php /** * The core plugin class file * * Defines the functions necessary to register our custom taxonomies with * WordPress. * * @link http://code.tutsplus.com/series/the-beginners-guide-to-wordpress-taxonomies--cms-706 * @since 1.0.0 * * @package Custom_Taxonomies * @author Tom McFarlin <tom@tommcfarlin.com> */ class My_Custom_Taxonomies { /** * Initializes the plugin by registering the hooks necessary * for creating our custom taxonomies within WordPress. * * @since 1.0.0 */ public function init() { } }
After that, let's go back to my-custom-taxonomies.php
and add code to include the file and the methods that create an instance of the class and execute it: p>
<?php /** Loads the custom taxonomy class file. */ require_once( dirname( __FILE__ ) . '/class-my-custom-taxonomies.php' ); /** * Creates an instance of the My_Custom_Taxonomies class * and calls its initialization method. * * @since 1.0.0 */ function custom_taxonomies_run() { $custom_tax = new My_Custom_Taxonomies(); $custom_tax->init(); } custom_taxonomies_run();
Now we have everything we need to start actually setting up hooks and callbacks to create custom taxonomies.
At this point, we are ready to start introducing our taxonomy. We first focus on two hierarchical taxonomies - Photo and Video.
In the class body of the class-my-custom-taxonomies.php
file, add the following function:
<?php /** * Creates the Photographs taxonomy that appears on all Post dashboard * pages. * * @since 1.0.0 */ public function init_photographs() { $labels = array( 'name' => 'Photographs', 'singular_name' => 'Photograph', 'edit_item' => 'Edit Photograph', 'update_item' => 'Update Photograph', 'add_new_item' => 'Add New Photograph', 'menu_name' => 'Photographs' ); $args = array( 'hierarchical' => true, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'photograph' ) ); register_taxonomy( 'photograph', 'post', $args ); }
This function is responsible for creating the Photo category and will be called from the init function at the appropriate time.
Now, let’s do the same thing with video :
<?php /** * Creates the Videos taxonomy that appears on all Post dashboard * pages. * * @since 1.0.0 */ public function init_videos() { $labels = array( 'name' => 'Videos', 'singular_name' => 'Video', 'edit_item' => 'Edit Video', 'update_item' => 'Update Video', 'add_new_item' => 'Add New Video', 'menu_name' => 'Videos' ); $args = array( 'hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'video' ) ); register_taxonomy( 'video', 'post', $args ); }
Let's call these two functions in the init function. We do this by registering these functions using the init
hook provided by WordPress:
<?php public function init() { add_action( 'init', array( $this, 'init_photographs' ) ); add_action( 'init', array( $this, 'init_videos' ) ); }
Here we should be able to go to Add New Post and see the new category options in the dashboard. If not, please double check your code against the code shared above.
Now that we've covered our hierarchical taxonomy, let's move on to our Video Types - or our non-hierarchical - taxonomy.
这实际上与我们到目前为止编写的代码没有太大不同。实际上,主要区别在于,我们不是将 hierarchical
指定为 true
,而是将其设置为 false
。
<?php /** * Creates the Film Type taxonomy that appears on all Post dashboard * pages. * * @since 1.0.0 */ public function init_film_type() { $labels = array( 'name' => 'Film Type', 'singular_name' => 'Film Type', 'edit_item' => 'Edit Film Type', 'update_item' => 'Update Film Type', 'add_new_item' => 'Add New Film Type', 'menu_name' => 'Film Type' ); $args = array( 'hierarchical' => false, 'labels' => $labels, 'show_ui' => true, 'show_admin_column' => true, 'rewrite' => array( 'slug' => 'film-type' ) ); register_taxonomy( 'film-type', 'post', $args ); }
这将导致不同类型的用户界面元素,看起来更像标签,而不是您在上面看到的类别选项。
最后,将以下行与其余钩子一起添加到 init 方法中:
<?php add_action( 'init', array( $this, 'init_film_type' ) );
请注意,函数更像是分类标签。再次重申,这是分层分类法和非分层分类法的主要区别之一。
现在我们准备好试用该插件了。假设您正确遵循了本教程中的所有内容,那么您应该能够创建新帖子,并使用照片类型或视频类型对其进行标记作为影片的一种类型,并在保存或更新您的帖子后保留更改。
如果没有,请根据此处引用的内容以及关联的 GitHub 存储库中引用的内容仔细检查您的代码。
WordPress 分类法初学者指南到此结束。在整个系列中,我们广泛了解了分类法的定义、它们在 WordPress 中扮演的角色,甚至还实现了一些我们自己的分类法。
此时,您应该对这个概念以及如何将它们包含在您的下一个项目中有深入的了解。
如果没有,请随时在下面的字段中留下问题、评论或一般反馈。
The above is the detailed content of Custom Plugins: A Comprehensive Guide to WordPress Taxonomies for Beginners. For more information, please follow other related articles on the PHP Chinese website!