Skip to main content
  1. All Posts/

wordpress-plugin-tools

Tools Open Source PHP WordPress

WordPress Plugin Tools

The goal of this library is to simplify the proces of creating custom post types and settings pages in WordPress.

Installation

You can install the package via composer:

composer require martijnvdb/wordpress-plugin-tools

Usage

All documented objects use a fluent interface, which allows you to chain methods. For example:

$custom_posttype = PostType::create('custom-posttype')
    ->setDescription('A very interesting description')
    ->setSlug('custom-slug')
    ->setIcon('dashicons-thumbs-up')
    ->setPublic()
    ->build();

Every chain should end with a build() method. The build() method will register the object using the WordPress action hooks. Except when using an object as an argument in a method. In the following example, the CustomField object should not end with the build() method:

$custom_field = CustomField::create('custom-field')
    ->setType('textarea')
    ->setLabel('page-custom-textarea');

$custom_metabox = MetaBox::create('custom-metabox')
    ->addPostType('page')
    ->addCustomField($custom_field)
    ->build();

PostType

This object allows you to easily create one or multiple post types without having to worry about the WordPress hooks. Chain any method you like and end with the build() method to register the post type.

Create a new PostType

$custom_posttype = PostType::create('custom-posttype')->build();

Add a MetaBox to the PostType

MetaBoxes on their own won’t be shown anywhere. They have to be added to a PostType. These methods will do exactly that.

$first_metabox = MetaBox::create('first-metabox');
$second_metabox = MetaBox::create('second-metabox');
$third_metabox = MetaBox::create('third-metabox');

$custom_posttype = PostType::create('custom-posttype')
    ->addMetaBox($first_metabox) // Add a single MetaBox
    ->addMetaBoxes([$second_metabox, $third_metabox])// Or add multiple at once
    ->build();

Add labels to the PostType

All the WordPress labels can be used. See a full list of supported labels.

$products_posttype = PostType::create('products')
    ->setLabel('name', 'Products') // Add a single label

    // Or add multiple at once
    ->setLabels([
        'singular_name' => 'Product',
        'add_new_item' => 'Add new Product',
        'add_new' => 'New Product',
        'edit_item' => 'Edit Product',
    ])
    ->build();

Add a description to the PostType

$custom_posttype = PostType::create('custom-posttype')
    ->setDescription('A very interesting description')
    ->build();

Make the PostType public

PostTypes are false by default. Using this method the PostType will be shown in the admin interface.

$custom_posttype = PostType::create('custom-posttype')
    ->setPublic()
    ->build();

Set menu position of the PostType

$custom_posttype = PostType::create('custom-posttype')
    ->setMenuPosition(8)
    ->build();

Set the icon of the PostType

$custom_posttype = PostType::create('custom-posttype')
    ->setIcon('dashicons-thumbs-up')
    ->build();

Add feature support to the PostType

Any WordPress core feature can be used. The core features are title, editor, comments, revisions, trackbacks, author, excerpt, page-attributes, thumbnail, custom-fields and post-formats.

$custom_posttype = PostType::create('custom-posttype')
    ->addSupport(['title', 'thumbnail', 'comments']) // Must be an array
    ->build();

See a full list of supported features

Remove feature support from the PostType

Any WordPress core feature can be removed. The core features are title, editor, comments, revisions, trackbacks, author, excerpt, page-attributes, thumbnail, custom-fields and post-formats.

$custom_posttype = PostType::create('custom-posttype')
    ->removeSupport(['editor']) // Must be an array
    ->build();

See a full list of supported features

Set the slug of the PostType

$custom_posttype = PostType::create('custom-posttype')
    ->setSlug('custom-slug')
    ->build();

Use the block editor in the PostType

$custom_posttype = PostType::create('custom-posttype')
    ->addBlockEditor()
    ->build();

Add any supported option to the PostType

This library only has a handfull of dedicated methods to set post type options. To use any other post type option you can use the addOption() method. See a full list of possible options.

$custom_posttype = PostType::create('custom-posttype')
    // Some examples
    ->addOption('show_in_admin_bar', false)
    ->addOption('show_in_nav_menus', false)
    ->addOption('has_archive', true)
    ->build();

CustomField

This object allows you to easily create one or multiple custom fields without having to worry about the WordPress hooks. Chain any method you like and end with the build() method to register the custom field.

Create a new CustomField

$customfield = CustomField::create('new-customfield')->build();

Set the CustomField type

The possible CustomField types are text, textarea, checkbox, number, select, radio and editor.

$new_customfield = CustomField::create('new-customfield')
   ...