2017 © Pedro Peláez
 

library wp-custom-post-type-class

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

image

jjgrainger/wp-custom-post-type-class

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

  • Friday, May 19, 2017
  • by jjgrainger
  • Repository
  • 44 Watchers
  • 345 Stars
  • 15,618 Installations
  • PHP
  • 5 Dependents
  • 0 Suggesters
  • 97 Forks
  • 12 Open issues
  • 13 Versions
  • 9 % Grown

The README.md

N.B I've released an updated version of the project to a new repository, PostTypes., (*1)

WP Custom Post Type Class v1.4

Latest Stable Version Total Downloads License, (*2)

A single class to help you build more advanced custom post types quickly., (*3)

Installation

Install with Composer

Add the package to your projects composer.json file. Visit getcomposer.org more information., (*4)

{
    "require": {
        "jjgrainger/wp-custom-post-type-class": "dev-master"
    }
}

Install Manually

Download and include the class file into your themes functions.php like so:, (*5)

include_once('CPT.php');

and your ready to roll!, (*6)

Creating a new Custom Post type

To create the post type simply create a new object, (*7)

$books = new CPT('book');

The first parameter is the post type name and is required. ideally the post type name is all lowercase and words separated with an underscore _., (*8)

to be specific about other post types names you can pass an associative array:, (*9)

post_type_name - the name of post type (singular, lowercase, underscores), (*10)

singular - the singular label of the post type (Book, Person), (*11)

plural - the plural of the post type (Books, People), (*12)

slug - the permalink slug for the post type (plural, lowercase, hyphens), (*13)

you pass these names through the first parameter as an array like so:, (*14)

$people = new CPT(array(
    'post_type_name' => 'person',
    'singular' => 'Person',
    'plural' => 'People',
    'slug' => 'people'
));

The optional second parameter is the arguments for the post_type. see WordPress codex for available options., (*15)

The Class uses the WordPress defaults where possible., (*16)

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:, (*17)

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

See the WordPress codex for all available options., (*18)

Existing Post Types

To work with exisiting post types, simply pass the post type name into the class constructor, (*19)

$blog = new CPT('post');

Adding Taxonomies

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

$books->register_taxonomy('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:, (*21)

$books->register_taxonomy(array(
    'taxonomy_name' => 'genre',
    'singular' => 'Genre',
    'plural' => 'Genres',
    'slug' => 'genre'
));

Again options can be passed optionally as an array. see the WordPress codex for all possible options., (*22)

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., (*23)

Admin Edit Screen

Filters

When you register a taxonomy it is automagically added to the admin edit screen as a filter and a column., (*24)

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

$books->filters(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., (*26)

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., (*27)

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., (*28)

This class doesn't have any methods for adding custom fields as Advanced Custom Fields (ACF) is way more awesome than anything this class could do!, (*29)

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

$books->columns(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:, (*31)

  • 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., (*32)

You do so with the populate_column() method like so:, (*33)

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

    // your code goes here…

});

so we can populate our price column like so:, (*34)

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

    echo "£" . get_field('price'); // ACF get_field() function

});

The method will pass two variables into the function:, (*35)

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

These are passed to help you populate the column appropriately., (*36)

Sorting Columns

If it makes sense that column should be sortable by ascending/descending you can define custom sortable columns like so:, (*37)

$books->sortable(array(
    'column_name' => array('meta_key', true)
));

The true/false is used to define whether the meta value is a string or integer, reason being is that if numbers are ordered as a string, numbers such as:, (*38)

1, 3, 5, 11, 14, 21, 33

Would be ordered as:, (*39)

1, 11, 14, 21, 3, 33, 5

By adding the option true value the values will be sorted as integers, if false or undefined, the class will sort columns as string., (*40)

so for our books example you will use:, (*41)

$books->sortable(array(
    'price' => array('price', true),
    'rating' => array('rating', true)
));

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 menu_icon() method like so:, (*42)

$books->menu_icon("dashicons-book-alt");

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

Translation

The class is setup for translation, but if you need to set your own textdomain to work with your theme or plugin use the set_textdomain() method:, (*44)

$books->set_textdomain('your-textdomain');

Flush Rewrite Rules

You can programmatically recreate the sites rewrite rules with the flush() method. This is an expensive operation and should be used with caution, see codex for more., (*45)

$books->flush();

Notes

Author

Joe Grainger * http://jjgrainger.co.uk * http://twitter.com/jjgrainger, (*46)

The Versions

19/05 2017

dev-master

9999999-dev https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

11/08 2015

v1.4

1.4.0.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

11/08 2015

dev-v1.4-rc1

dev-v1.4-rc1 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

10/08 2015

dev-fix-add-admin-columns-error

dev-fix-add-admin-columns-error https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

10/08 2015

dev-add-updated-messages

dev-add-updated-messages https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

31/05 2015

v1.3.3

1.3.3.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

30/05 2015

1.3.2

1.3.2.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

29/03 2015

v1.3.1

1.3.1.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

11/12 2014

v1.3

1.3.0.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

13/06 2014

v1.2.3

1.2.3.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

06/06 2014

v1.2.2

1.2.2.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

13/03 2014

v1.2.1

1.2.1.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

04/02 2014

v1.2

1.2.0.0 https://github.com/jjgrainger/wp-custom-post-type-class

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

  Sources   Download

MIT

The Requires

  • php >=5.3.0