Skip to content

Component Taxonomy

alex_prokopenko edited this page Jan 17, 2018 · 2 revisions

Taxonomy base class is used to make registration of taxonomies more easy and more intuitive.

Taxonomy base class is defined in Objects\Taxonomy.php class.

Why do you need to register a taxonomy inside the code?

You can say, that there are a lot of plugins, which has admin interface to register a taxonomies and post type. -Yes, it's true. But let's check a bit deeper what we get with an additional plugin.

Each plugin with admin interface usually include a lot of code during the page load. To register a new post type it needs to check options from the database, parse them (unserialize) and only then it calls a registration function. Furthermore, good plugins has a lot of hooks inside, which added to make this plugin more flexible. And all this staff adds some time in your page load time. Good, optimized WordPress site has more than 10 3rd-party plugins. WordPress is not the fastest CMS in the world and you want to slow it down with one more plugin, because you're too lazy to write 20-30 lines of code?

Even if you use a plugin - all of them just repeat standard WordPress options and do not optimize anything in WordPress post types registration process.

We think registering taxonomies and post types inside the code for your theme is the only correct way of the development of a high-quality theme. That's why we prepared a base class to register a post type very fast and optimize some non-trivial options of WordPress register_taxonomy() function.

Usage instructions

To create a new taxonomy you need to create your own class and extends it from JustCoded\WP\Framework\Objects\Taxonomy. Inside init() method you should define Taxonomy correct settings (with class properties) and call $this->register() at the end of the init().

Example:

<?php
namespace Boilerplate\Theme\Taxonomy;

use JustCoded\WP\Framework\Objects\Taxonomy;
use Boilerplate\Theme\Post_Type\Employee;

/**
 * Class Department Taxonomy
 *
 * @package Boilerplate\Theme\Department
 */
class Department extends Taxonomy {
	/**
	 * ID
	 *
	 * @var string
	 */
	public static $ID = 'department';

	/**
	 * Rewrite URL part
	 *
	 * @var string
	 */
	public static $SLUG = 'department';

	/**
	 * Registration function
	 */
	public function init() {
		$this->label_singular = 'Department';
		$this->label_multiple = 'Departments';
		$this->textdomain     = 'boilerplate';

		$this->is_hierarchical  = false;
		$this->has_single       = true;
		$this->rewrite_singular = false;

		$this->has_admin_menu = true;

		$this->post_types = array(
			Employee::$ID,
		);

        // IT'S IMPORTANT TO CALL THIS METHOD, BECAUSE IT CALLS register_taxonomy() inside!
		$this->register();
	}
}

Most of the class properties duplicates configuration data you can pass to WordPress register_taxonomy() function. However there are few properties which makes easier to set correct values for some non-trivial options.

Activate registration

By default when you created a new class - it's not included anywhere inside the code and taxonomy won't be added.

To do this - you need to create a new instance of your new class. This class implements Singleton design pattern, so creating an instance is available only through static instance() method.

If you use Theme class to register your theme features, then the code will be like this:

	/**
	 * Register taxonomies
	 */
	public function register_taxonomies() {
		Department::instance();
	}

New properties

A lot of mistakes in registering of the post types is usually made around such arguments:

  • public
  • show_in_nav_menus
  • rewrite.with_front

Argument names are not very intuitive and understandable, without reading the documentation. We decide to make arguments much more intuitive. So we added logical properties, which are converted to the values for options above:

  • $has_single - means you have a singular template for your post type.
  • $rewrite_singular - make rewrite structure "singular" (do not include post type "slug" prefix).

Final arguments for register_taxonomy are generated with such peace of code:

<?php
...
$labels = array(
    'name'          => _x( $this->label_multiple, 'taxonomy general name', $this->textdomain ),
    'singular_name' => _x( $this->label_singular, 'taxonomy singular name', $this->textdomain ),
);

$args = array(
    'labels'       => $labels,
    'hierarchical' => $this->is_hierarchical,

    'public'            => $this->has_single,
    'show_in_nav_menus' => $this->has_single,

    'show_tagcloud' => $this->has_tag_cloud,

    'show_ui'            => $this->has_admin_menu,
    'show_admin_column'  => $this->admin_posts_column,
    'show_in_quick_edit' => $this->is_quick_editable,

    'query_var' => $this->query_var,
    'rewrite'   => array(
        'slug' => $this::$SLUG,
        'with_front' => ! $this->rewrite_singular,
        'hierarchical' => $this->rewrite_hierarchical,
    ),
);

if ( ! empty( $this->capabilities ) ) {
    $args['capabilities'] = $this->capabilities;
}

register_taxonomy( $this::$ID, null, $args );

Class reference

Properties

Property Type Default Description
static $ID string - Post_Type ID
static $SLUG string - Post_Type rewrite prefix, called slug
$label_singular string - Single Post Type label.
$label_multiple string - Multiple Post Type label.
$post_types array [] Post type IDs to be used with this taxonomy.
$has_single bool false Has single page or not.
$is_hierarchical bool false Allow hierarchical urls.
$rewrite_singular bool false Simplify URLs structure and remove "slug" prefix from URL.
$rewrite_hierarchical bool false aaaaaaa.
$has_admin_menu bool true Show in admin menu.
$is_quick_editable bool true Ability to edit in quick edit in posts list.
$admin_posts_column bool true Ability to show admin column in posts list.
$has_tag_cloud bool false Array of supported standard features of CPT.
*Adds title, editor, featured image and revisions by default.
$labels array [] Labels array to override auto-generated labels if needed.
$capabilities array null Custom capability options.
$query_var bool true Custom query var to be applied for single pages.
$textdomain string - Textdomain to apply to labels on post type registration.

Methods

Method / Called in Description
__construct Register init hook to run taxonomy registration.
init
(a)init
Abstract method, you should define your own init method to register a taxonomy.
register
init()
Main logic for registering a taxonomy based on class properties
views
(a)template_include
Registers logic to support template hierarchy views/{post-type}/taxonomy-{tax-id}.