Ever needed a feature so bad, but it’s not a part of the template? And it should be because it’s more of a template function. Well, you can always hack it… or rather create a plugin that will add the functionality that you will need to your WordPress installation. That’s definitely a better way of saying it.
Ever needed a feature so bad, but it’s not a part of the template? And it should be because it’s more of a template function. Well, you can always hack it… or rather create a plugin that will add the functionality that you will need to your WordPress installation. That’s definitely a better way of saying it.
And then the next thing we’re going to do is we’re going to add two taxonomies to that recipe. We’re going to add recipe category and we’re going to add recipe tags. So to start all this off, what we’re going to do is we’re going to create a plugin.
class MH_template_addon
{
protected static $instance = NULL;
public $post_type;
public $taxonomies;
/**
* Used for regular plugin work.
*
* @wp-hook plugins_loaded
* @return void
*/
public function plugin_setup()
{
}
/**
* Constructor, init the functions inside WP
*
* @since 1.0.0
* @return void
*/
public function __construct() {
//init and make recipe post type
$this->post_type = 'recipe';
add_action(
'plugins_loaded',
array ( $this, 'plugin_setup')
);
}
}
$mh_template_addons = new MH_template_addon();
The plugin is going to be class based so that we could use a singleton instance and we could make use of singletons. For this specific plugin we’re not going to be able to or we’re not going to use it for that purpose. So it doesn’t matter at this point but it’s always good practice to be able to write your plugins as singleton classes so that later on if you’d like to add an update you can do so.
PHP is a very versatile language that once it has been updated and the class inheritance was really formally introduced it took off and it makes the ease of writing plugins for WordPress and also writing PHP code much easier. So you’ll see that I’ve named mine MHTemplateAddons. You can name yours whatever you want.
But the first thing that we’re going to look into is we’re going to look into the construct. As you know, when you have a class, the first thing that the class looks for is the construct function. So in the construct we’re actually going to add this post type as recipe and add action plugins loaded which is just another WordPress hook to say hey we see that you’re loaded; or that plugins have loaded.
Now that you’ve loaded the plugins, which means that it’s gotten the code from the folders; it can then execute on those functions—because it’s physically loaded those files. And in the plugin setup, we’ll add three actions. One is going to be a body class because I would like to add a body class to the body to know that I have added this plugin to the set of plugins that are available.
I also want to get the taxonomies and the post type. Once I’ve done that I’ll move on down to the recipe taxonomies. You can take a quick look through what I’ve already put there, but it’s very standard the same thing with the categories.
And the last is the post type. You can take a quick look at it, but there isn’t much to know there. One last thing is I’m going to put a body class. For some styling and later function this will come in handy.
To review, its a filter function. WordPress has action and filters. Since it’s a filter hook it actually gets something (it gets the array of body classes). With this you’re adding to the array and then returning it back. It’s very important to understand the difference between hook actions and hook filters and this plugin uses both.
Find source code and more updates on github: mh-template-addons
We’re all ready. It’s really that simple. There isn’t much to this plugin again other than to add the taxonomy and the post type.
post_type = 'recipe';
add_action(
'plugins_loaded',
array ( $this, 'plugin_setup')
);
}
/**
* Handler for the action 'init'. Instantiates this class.
*
* @since 1.0.0
* @access public
* @return $instance
*/
public function get_object()
{
NULL === self::$instance and self::$instance = new self;
return self::$instance;
}
/**
* Setup Taxonomies
* Creates 'recipeentry_tag' and 'recipeentry_location' taxonomies.
* Enhance via filter `mh_template_addons_taxonomies`
*
* @uses register_taxonomy, apply_filters
* @since 1.0.0
* @return void
*/
function mh_register_recipe_taxonomies()
{
$mh_template_addons_taxonomies = array();
$labels = array(
'name' => _x( 'Recipe Entry Tags', 'taxonomy general name', 'mh_template_addons_plugin' ),
'singular_name' => _x( 'Recipe Entry Tag', 'taxonomy singular name', 'mh_template_addons_plugin' ),
'search_items' => __( 'Search Recipe Entry Tags', 'mh_template_addons_plugin' ),
'all_items' => __( 'All Recipe Entry Tags', 'mh_template_addons_plugin' ),
'parent_item' => __( 'Parent Recipe Entry Tag', 'mh_template_addons_plugin' ),
'parent_item_colon' => __( 'Parent Recipe Entry Tag:', 'mh_template_addons_plugin' ),
'edit_item' => __( 'Edit Recipe Entry Tag', 'mh_template_addons_plugin' ),
'update_item' => __( 'Update Recipe Entry Tag', 'mh_template_addons_plugin' ),
'add_new_item' => __( 'Add New Recipe EntryTag', 'mh_template_addons_plugin' ),
'new_item_name' => __( 'New Recipe Entry Tag Name', 'mh_template_addons_plugin' ),
'menu_name' => __( 'Entry Tags', 'mh_template_addons_plugin' ),
);
$args = array(
'hierarchical' => FALSE,
'labels' => $labels,
'show_ui' => TRUE,
'show_admin_column' => FALSE,
'query_var' => TRUE,
'rewrite' => TRUE,
'show_in_rest' => TRUE
);
$mh_template_addons_taxonomies[] = array(
'taxonomy' => 'recipeentry_tag',
'post_type' => 'recipeentry',
'args' => $args
);
// categories
$labels = array(
'name' => _x( 'Entry Category', 'taxonomy general name', 'mh_template_addons_plugin' ),
'singular_name' => _x( 'Entry Category', 'taxonomy singular name', 'mh_template_addons_plugin' ),
'search_items' => __( 'Search Entry Category', 'mh_template_addons_plugin' ),
'all_items' => __( 'All Entry Categories', 'mh_template_addons_plugin' ),
'parent_item' => __( 'Parent Entry Category', 'mh_template_addons_plugin' ),
'parent_item_colon' => __( 'Parent Entry Category:', 'mh_template_addons_plugin' ),
'edit_item' => __( 'EditEntry Category', 'mh_template_addons_plugin' ),
'update_item' => __( 'Update Entry Category', 'mh_template_addons_plugin' ),
'add_new_item' => __( 'Add New Entry Category', 'mh_template_addons_plugin' ),
'new_item_name' => __( 'New Entry Category Name', 'mh_template_addons_plugin' ),
'menu_name' => __( 'Entry Category', 'mh_template_addons_plugin' ),
);
$args = array(
'hierarchical' => TRUE,
'labels' => $labels,
'show_ui' => TRUE,
'query_var' => TRUE,
//'rewrite' => TRUE,
'rewrite' => array('slug' => 'recipe/cat', 'with_front' => false),
'public' => TRUE,
'show_in_rest' => TRUE,
'show_in_menu' => TRUE,
'meta_box_cb' => FALSE,
'show_admin_column' => FALSE,
);
$mh_template_addons_taxonomies[] = array(
'taxonomy' => 'recipeentry_category',
'post_type' => 'recipe',
'args' => $args
);
foreach ( $mh_template_addons_taxonomies as $attachment_taxonomy ) {
register_taxonomy(
$attachment_taxonomy['taxonomy'],
$attachment_taxonomy['post_type'],
$attachment_taxonomy['args']
);
}
}
function mh_template_addons_body_class($classes) {
$classes[] = 'mh-template-toolkit';
return $classes;
}
function mh_register_recipe_posttype() {
$labels = array(
'name' => _x( 'Recipe', 'post type general name' , 'mh_template_addons_plugin' ),
'singular_name' => _x( 'Recipe', 'post type singular name' , 'mh_template_addons_plugin' ),
'add_new' => _x( 'Add Entry', 'Recipe Entry' , 'mh_template_addons_plugin' ),
'add_new_item' => __( 'Add New Entry' , 'mh_template_addons_plugin' ),
'edit_item' => __( 'Edit entry' , 'mh_template_addons_plugin' ),
'new_item' => __( 'New entry' , 'mh_template_addons_plugin' ),
'all_items' => __( 'All Recipe Entries' , 'mh_template_addons_plugin' ),
'view_item' => __( 'View entry' , 'mh_template_addons_plugin' ),
'search_items' => __( 'Search entries' , 'mh_template_addons_plugin' ),
'not_found' => __( 'No entries found' , 'mh_template_addons_plugin' ),
'not_found_in_trash' => __( 'No entries found in the Trash' , 'mh_template_addons_plugin' ),
'parent_item_colon' => "’",
'menu_name' => 'Recipes'
);
$args = array(
'labels' => $labels,
'description' => 'A complete collection of all the entries that are published.',
'public' => true,
'menu_position' => 5,
'show_in_rest' => true,
'supports' => array( 'title', 'editor', 'thumbnail', 'excerpt', 'comments', 'custom-fields', 'revisions', 'author' ),
'has_archive' => true,
'rewrite' => array('slug' => 'recipe','with_front' => false),
'taxonomies' => array('recipeentry_location', 'recipeentry_tag'),
);
register_post_type( 'recipe', $args );
}
}
$mh_template_addons = new MH_template_addon();
Join us in our next segment where we’ll add the actual template piece to then make use of both our taxonomies and our post type.