Create An Image Slide Plugin Part 4: Create A Custom Taxonomy

Create An Image Slide Plugin Part 4: Create A Custom Taxonomy
Create An Image Slide Plugin Create A Custom Taxonomy

Picking off where we left off, in this post we’ll be talking about grouping two or more of our slides into a slider by creating a custom taxonomy.

In the previous post we talked about creating a custom content type for our slides an as we’ve seen, it is a pretty straight forward process involving naming and some settings for the new content we want to create.

What Is A Taxonomy?

Seriously, how many times have you heard the word taxonomy? đź™‚ It’s one of those words that you rarely hear and don’t know what they mean.

I hadn’t heard the word until I started working with WordPress plugins. Basically, a taxonomy is a classification, a grouping of some sorts. So it is in WordPress that it uses taxonomies to classify or group things together.

Taxonomies are used widely on any type of site or web application. We might use them to group types of animals on a zoo’s website or the many sports on a TV channel’s site. In a music app we might use taxonomies for music genres or the year of release for songs and albums.

Taxonomies can be hierarchical or not. In hierarchical taxonomies, the elements can have a parent and multiple children while in the non-hierarchical ones, the elements are arranged in a flat structure, each on the same level.

What Is A Taxonomy Term?

To actually group things together, a taxonomy must have some identifiers that we can assign to different pieces of content so that they relate to each other in some way.

In a WordPress taxonomy, those identifiers are called, terms.

On this blog we have a ‘Category’ taxonomy that has multiple terms like: ‘WordPress’, ‘Plugins’ or ‘Tutorials’.

The hierarchy that we’ve talked about in the previous section refers to the relationships between the terms of a taxonomy. So, they can have parents and children or they can be laid out flat, at the same level.

Default WordPress Taxonomies

Out of the box, WordPress offers two taxonomies:

  • categories
  • tags

A third taxonomy can be used by adding support for it: post formats.

Categories are usually a broad way of grouping posts together and most of the times are defined before adding the content to a site, with the possibility of adding new ones, of course.

Tags, on the other hand, tend to be more specific and are usually set at the time of writing the content. They are more specific to the topic that you’ve written about.

A common practice is to have few categories like one, two or three at the most while tags can be much more numerous.

Creating A Custom Taxonomy

If you want to follow along I’ve set up a GitHub repository where you’ll find the code from this series. The finished code for this post is in the part_4 branch. If you want to follow along please download the part_3 branch.

Just like in the previous post where we created a custom content type, the process of creating a custom taxonomy is pretty straightforward.

Here is the process we’ll go through:

  • We’ll use the register_taxonomy() built-in function to tell WordPress about the new taxonomy.
  • We’ll use the init hook as the the moment in the WordPress loading sequence to make the new taxonomy available.
  • The new taxonomy name will be mnsp_slider, as we’ll group multiple slides into an animated slideshow or slider.

As with the custom content type, we’ll create a new file called /admin/class-monospace-slides-tax.php. As you can see it is located in the /admin folder.

Register A Custom Taxonomy In WordPress

Open the newly created file and add the following code:

<?php
/**
 * The admin-specific functionality for creating a custom taxonomy.
 *
 * @link       https://www.mikeinmonospace.com/
 * @since      1.0.0
 *
 * @package    Monospace_Slides
 * @subpackage Monospace_Slides/admin
 */

/**
 * The admin-specific functionality for creating a custom taxonomy.
 *
 * Defines all the functions needed to register and manage a custom taxonomy.
 *
 * @package    Monospace_Slides
 * @subpackage Monospace_Slides/admin
 * @author     Mike In Monospace <mikeinmonospace@gmail.com>
 */
class Monospace_Slides_TAX {

	/**
	 * Registers a new taxonomy for a custom post type.
	 *
	 * @since  1.0.0
	 * @access public
	 * @uses   register_taxonomy()
	 */
	public static function new_tax_slider() {

		$tax_name = 'mnsp_slider';

		$labels = array(
			'name'              => esc_html_x( 'Sliders', 'taxonomy general name', 'monospace-slides' ),
			'singular_name'     => esc_html_x( 'Slider', 'taxonomy singular name', 'monospace-slides' ),
			'search_items'      => esc_html__( 'Search Sliders', 'monospace-slides' ),
			'all_items'         => esc_html__( 'All Sliders', 'monospace-slides' ),
			'parent_item'       => esc_html__( 'Parent Slider', 'monospace-slides' ),
			'parent_item_colon' => esc_html__( 'Parent Slider:', 'monospace-slides' ),
			'edit_item'         => esc_html__( 'Edit Slider', 'monospace-slides' ),
			'update_item'       => esc_html__( 'Update Slider', 'monospace-slides' ),
			'add_new_item'      => esc_html__( 'Add New Slider', 'monospace-slides' ),
			'new_item_name'     => esc_html__( 'New Slider Name', 'monospace-slides' ),
			'menu_name'         => esc_html__( 'Sliders', 'monospace-slides' ),
		);

		$args = array(
			'labels'             => $labels,
			'description'        => esc_html__( 'Group multiple slides into a slider', 'monospace-slides' ),
			'hierarchical'       => true,
			'public'             => true,
			'publicly_queryable' => false,
			'show_ui'            => true,
			'show_in_menu'       => true,
			'show_in_nav_menus'  => false,
			'show_in_rest'       => true,
			'show_tagcloud'      => false,
			'show_in_quick_edit' => true,
			'show_admin_column'  => true,
		);

		$args = apply_filters( 'monospace_slides_taxonomy_options', $args );

		register_taxonomy( $tax_name, array( 'mnsp_slide' ), $args );

	}

}

Looking at the code you’ll notice a method declaration, new_tax_slider() that registers the new custom taxonomy.

Just like in the previous post, the method uses two array variables to define all the necessary stuff for the new taxonomy. At the end it uses the buil-in register_taxonomy() function to tell WordPress about the taxonomy.

Next, we have to tell WordPress about the new file we created as it doesn’t know it exists yet.

So, open the /includes/class-monospace-slides.php file and in the load_dependencies() method add the following code:

/**
 * The class responsible for the functionality of the custom taxonomy.
 */
require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-monospace-slides-tax.php';

This tells WordPress to load that file as we’ll use functionality from there.

Next, we’ll create a function in this same file that will contain all the hooks for the the new custom content type.

In the __construct method of this file add one line of code add the end:

$this->define_tax_hooks();

This tells WordPress to look for a method called define_cpt_hooks() in this file and execute that code.

For the new custom taxonomy to actually get registered we have to tell WordPress to run the new_tax_slider() method we created earlier in the Monospace_Slides_TAX class.

So, in this same file let’s create the define_tax_hooks() method we declared above. Add the following code after the define_cpt_hooks() method:

/**
 * Register all of the hooks related to registering a custom taxonomy.
 *
 * @since    1.0.0
 * @access   private
 */
private function define_tax_hooks() {

	$plugin_tax = new Monospace_Slides_TAX();

	$this->loader->add_action( 'init', $plugin_tax, 'new_tax_slider' );

}

The code above tells WordPress to look or a function called new_tax_slider inside the Monospace_Slides_TAX class and run it at the init moment in it’s initialization process.

Now, if we add the new code to our installed plugin and we pop in on the admin side we can see a new menu item for our custom content type, just like in the screenshot below:

Also each slide post will have a new meta box that will allow us to assign a slide to a specific slider.

So, there you have it. An easy way to group multiple slides into a slider.

Next, we’ll declare the new taxonomy in the /includes/class-monospace-slides-activator.php file and flush the rewrite rules afterwards.

So, open that file and in the activate() method, after the Monospace_Slides_CPT::new_cpt_slide() method call, add the following code:

Monospace_Slides_TAX::new_tax_slider();

That’s it, now we have a complete custom taxonomy functionality in our plugin.

At The End

We’ve gone through another post in which we covered quite a lot of ground. We’ve learned how to classify the posts from our newly created content type, the mnsp_slide, into sliders with the help of taxonomies.

Using the mnsp_slider taxonomy we’ll later be able to display multiple slides into an animated slideshow and by using different taxonomy terms we can have multiple slideshows with different sides in them.

Until the next post in this series please share your feedback or thoughts on what you’d like to see covered in more depth or things I may have missed.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.