jazzman/custom-post-type

A single class to help you build more advanced custom post types quickly.

Installs: 1 149

Dependents: 0

Suggesters: 0

Security: 0

Stars: 3

Watchers: 3

Forks: 1

Open Issues: 2

Type:wordpress-muplugin

3.3.5 2023-08-02 11:55 UTC

README

Codacy Badge A single class to help you build more advanced custom post types quickly.

Installation

composer require jazzman/custom-post-type

require_once ABSPATH.'vendor/autoload.php';

Creating a new Custom Post type

To create the post type simply create a new object

use JazzMan\Post\CustomPostType;

$books = new CustomPostType('book');

The optional second parameter is the arguments for the post_type. see WordPress codex for available options.

The Class uses the WordPress defaults where possible.

To override the default options simply pass an array of options as the second parameter. Not all options have to be passed just the ones you want to add/override like so:

$books = new CustomPostType('book', array(
	'supports' => array('title', 'editor', 'thumbnail', 'comments')
));

See the WordPress codex for all available options.

Existing Post Types

To work with exisiting post types, simply pass the post type name into the class constructor

$blog = new CustomPostType('post');

Adding Taxonomies

You can add taxonomies easily using the register_taxonomy() method like so:

$books->registerTaxonomy('genres');

this method accepts two arguments, names and options. The taxonomy name is required and can be string (the taxonomy name), or an array of names following same format as post types:

$books->registerTaxonomy('genres',array(
	'show_ui'       => true,
	'query_var'     => true,
	'rewrite'       => array( 'slug' => 'the_genre' )
));

Again options can be passed optionally as an array. see the WordPress codex for all possible options.

Existing Taxonomies

You can add exisiting taxonomies to the post type by passing the taxonomy name through the register_taxonomy method. You will only need to specify the options for the custom taxonomy once, when its first registered.

Admin Edit Screen

Filters

When you register a taxonomy it is automagically added to the admin edit screen as a filter and a column.

You can define what filters you want to appear by using the filters() method:

$books->setFilters(array('genre'));

By passing an array of taxonomy names you can choose the filters that appear and the order they appear in. If you pass an empty array, no drop down filters will appear on the admin edit screen.

Columns

The Class has a number of methods to help you modify the admin columns. Taxonomies registered with this class are automagically added to the admin edit screen as columns.

You can add your own custom columns to include what ever value you want, for example with our books post type we will add custom fields for a price and rating.

You can define what columns you want to appear on the admin edit screen with the setColumns() method by passing an array like so:

$books->setColumns(array(
	'cb' => '<input type="checkbox" />',
	'title' => __('Title'),
	'genre' => __('Genres'),
	'price' => __('Price'),
	'rating' => __('Rating'),
	'date' => __('Date')
));

The key defines the name of the column, the value is the label that appears for that column. The following column names are automagically populated by the class:

  • any taxonomy registered through the object
  • cb the checkbox for bulk editing
  • title the post title with the edit link
  • author the post author
  • post_id the posts id
  • icon the posts thumbnail

Populating Columns

You will need to create a function to populate a column that isn't automagically populated.

You do so with the setPopulateColumns() method like so:

$books->setPopulateColumns('column_name', function($column, $post) {

	// your code goes here…

});

so we can populate our price column like so:

$books->setPopulateColumns('price', function($column, $post) {

	echo "£" . get_post_meta($post->ID,'price',true);

});

The method will pass two variables into the function:

  • $column - The column name (not the label)
  • $post - The current post object

These are passed to help you populate the column appropriately.

Menu Icons

Dashicons

With WordPress 3.8 comes dashicons an icon font you can use with your custom post types. To use simply pass the icon name through the setMenuIcon() method like so:

$books->setMenuIcon('dashicons-book-alt');

For a full list of icons and the class names to use visit https://developer.wordpress.org/resource/dashicons/