The goal of this library is to simplify the proces of creating custom post types and settings pages in Wordpress.
You can install the package via composer:
composer require martijnvdb/wordpress-plugin-tools
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();
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.
$custom_posttype = PostType::create('custom-posttype')->build();
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();
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();
$custom_posttype = PostType::create('custom-posttype')
->setDescription('A very interesting description')
->build();
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();
$custom_posttype = PostType::create('custom-posttype')
->setMenuPosition(8)
->build();
$custom_posttype = PostType::create('custom-posttype')
->setIcon('dashicons-thumbs-up')
->build();
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
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
$custom_posttype = PostType::create('custom-posttype')
->setSlug('custom-slug')
->build();
$custom_posttype = PostType::create('custom-posttype')
->addBlockEditor()
->build();
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();
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.
$customfield = CustomField::create('new-customfield')->build();
The possible CustomField types are text
, textarea
, checkbox
, number
, select
, radio
and editor
.
$new_customfield = CustomField::create('new-customfield')
->setType('textarea')
->build();
$new_customfield = CustomField::create('new-customfield')
->setLabel('New custom field')
->build();
This will only be used if the CustomField is a select
or radio
type.
$new_customfield = CustomField::create('new-customfield')
->setType('select') // Or 'radio'
->addOption('first-option', 'This is the first option')
->addOption('second-option', 'This is the second option')
// Or add multiple at once
->addOptions([
'third-option' => 'This is the third option',
'fourth-option' => 'This is the fourth option'
]);
->build();
This will only be used if the CustomField is a number
type.
$new_customfield = CustomField::create('new-customfield')
->setType('number')
->setMin(0)
->build();
This will only be used if the CustomField is a number
type.
$new_customfield = CustomField::create('new-customfield')
->setType('number')
->setMax(100)
->build();
This will only be used if the CustomField is a number
type.
$new_customfield = CustomField::create('new-customfield')
->setType('number')
->setStep(10)
->build();
This object allows you to easily create one or multiple metaboxes without having to worry about the Wordpress hooks. Chain any method you like and end with the build()
method to register the metabox.
$custom_metabox = MetaBox::create('custom-metabox')->build();
$custom_metabox = MetaBox::create('custom-metabox')
->setTitle('Metabox title')
->build();
$first_customfield = CustomField::create('first-customfield');
$second_customfield = CustomField::create('second-customfield');
$third_customfield = CustomField::create('third-customfield');
$custom_metabox = MetaBox::create('custom-metabox')
->addCustomField($first_customfield)
// Or add multiple at once
->addCustomFields([
$second_customfield,
$third_customfield,
])
->build();
This library allows you to easily create a growable and reorderable list of items. Each item in the list can contain multiple CustomFields. If for example you would like to add multiple URLs with a title and a description to a post, you can use a list for this.
$first_customfield = CustomField::create('first-customfield');
$second_customfield = CustomField::create('second-customfield');
$third_customfield = CustomField::create('third-customfield');
$custom_metabox = MetaBox::create('custom-metabox')
->addList('custom-list', [
$first_customfield,
$second_customfield,
$third_customfield
])
->build();
The ID of the post types has to be used as an argument of these methods. These methods allow you to add MetaBoxes to existing post types which aren't created with this library.
$custom_metabox = MetaBox::create('custom-metabox')
->addPostType('page')
// Or add multiple at once
->addPostTypes([
'custom-posttype',
'another-posttype',
])
->build();
This library uses two text strings in the MetaBox which can be customized or translated. The following texts are used:
new
Newdelete_confirm
Are you sure you want to delete this item?
This is how to customize them:
$custom_metabox = MetaBox::create('custom-metabox')
->setText('new', 'Nieuwe lijst')
// Or customize multiple at once
->setTexts([
'new' => 'Nieuwe lijst',
'delete_confirm' => 'Weet u zeker dat u deze lijst wil verwijderen?'
])
->build();
This object allows you to easily create one or multiple setting pages without having to worry about the Wordpress hooks. Chain any method you like and end with the build()
method to register the setting page.
$custom_settingspage = SettingsPage::create('custom-settingspage')->build();
$custom_settingspage = SettingsPage::create('custom-settingspage')
->setPageTitle('The page title')
->build();
$custom_settingspage = SettingsPage::create('custom-settingspage')
->setMenuTitle('Menu title')
->build();
$custom_settingspage = SettingsPage::create('custom-settingspage')
->setSlug('settingspage-slug')
->build();
$custom_settingspage = SettingsPage::create('custom-settingspage')
->setIcon('dashicons-thumbs-up')
->build();
$first_customfield = CustomField::create('first-customfield');
$second_customfield = CustomField::create('second-customfield');
$third_customfield = CustomField::create('third-customfield');
$custom_settingspage = SettingsPage::create('custom-settingspage')
->addCustomField($first_customfield)
// Or add multiple at once
->addCustomFields([
$second_customfield,
$third_customfield,
])
->build();