jjgrainger / PostTypes

Simple WordPress custom post types.
https://posttypes.jjgrainger.co.uk/
MIT License
373 stars 47 forks source link

v2.0 #18

Closed jjgrainger closed 6 years ago

jjgrainger commented 6 years ago

v2.0 contains a lot of changes, fixes and features. For those of you upgrading from v1.1.2 here are some of the key changes to consider.

The register method

When creating post types and taxonomies you will now need to register them to WordPress using the register() method. This method calls all WordPress hooks responsible for registering a custom post type to WordPress.

// Create a PostType object for books
$books = new PostType('books');

// Register the books post type to WordPress
$books->register();

Taxonomies

Taxonomies are now created with the Taxonomy class and has the same methods and properties as PostTypes, including columns.

// Create a Taxonomy object for genres
$genres = new Taxonomy('genre');

// Add the genre taxonomy to the books post type
$genres->posttype('book');

// Create a custom column for the taxonomy list table
$genres->columns()->add([
    'popularity' => __('Popularity')
]);

// Register the taxonomy to WordPress
$genres->register();

Translations

Previously, PostTypes used a variable to set the textdomain for translatable strings. Based on the WordPress documentation, this vairable has been removed and PostTypes no longer uses any gettext functions.

To translate strings when using PostTypes, you can use WordPress gettext functions when setting any names or labels.

$books = new PostType([
    'name'  => 'book',
    'singular' => __('Book', 'my-plugin'),
    'plural' => __('Books', 'my-plugin'),
    'slug' => 'book'
]);

$books->labels([
    'add_new' => __('Add New', 'my-plugin'),
    'add_new_item' => __('Add New Book', 'my-plugin'),
    'edit_item' => __('Edit Book', 'my-plugin'),
    'new_item' => __('New Book', 'my-plugin'),
    'view_item' => __('View Book', 'my-plugin'),
    'search_items' => __('Search Books', 'my-plugin'),
    'not_found' => __('No Books found', 'my-plugin'),
    'not_found_in_trash' => __('No Books found in Trash', 'my-plugin'),
    'parent_item_colon' => __('Parent Book:', 'my-plugin'),
]);

Changelog