martijnvdb / wordpress-plugin-tools
一个用于快速创建Wordpress插件的简单库
Requires
- twig/twig: ^3.0
This package is auto-updated.
Last update: 2024-09-29 02:23:56 UTC
README
此库的目标是简化在Wordpress中创建自定义文章类型和设置页面的过程。
安装
您可以通过composer安装此包
composer require martijnvdb/wordpress-plugin-tools
用法
所有文档对象都使用流畅的接口,这允许您链接方法。例如
$custom_posttype = PostType::create('custom-posttype') ->setDescription('A very interesting description') ->setSlug('custom-slug') ->setIcon('dashicons-thumbs-up') ->setPublic() ->build();
每个链都应该以build()
方法结束。build()
方法将使用Wordpress操作钩子注册对象。除非在方法中将对象作为参数使用。在以下示例中,CustomField对象不应以build()
方法结束
$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
此对象允许您轻松创建一个或多个文章类型,而无需担心Wordpress钩子。链接任何您喜欢的链式方法,并以build()
方法注册文章类型。
创建一个新的PostType
$custom_posttype = PostType::create('custom-posttype')->build();
向PostType添加MetaBox
MetaBox本身不会在任何地方显示。它们必须添加到文章类型中。这些方法会做到这一点。
$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();
向PostType添加标签
所有Wordpress标签都可以使用。请参阅支持的标签完整列表。
$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();
向PostType添加描述
$custom_posttype = PostType::create('custom-posttype') ->setDescription('A very interesting description') ->build();
使PostType公开
PostType默认为false
。使用此方法,PostType将在管理界面中显示。
$custom_posttype = PostType::create('custom-posttype') ->setPublic() ->build();
设置PostType的菜单位置
$custom_posttype = PostType::create('custom-posttype') ->setMenuPosition(8) ->build();
设置PostType的图标
$custom_posttype = PostType::create('custom-posttype') ->setIcon('dashicons-thumbs-up') ->build();
向PostType添加功能支持
任何Wordpress核心功能都可以使用。核心功能包括title
、editor
、comments
、revisions
、trackbacks
、author
、excerpt
、page-attributes
、thumbnail
、custom-fields
和post-formats
。
$custom_posttype = PostType::create('custom-posttype') ->addSupport(['title', 'thumbnail', 'comments']) // Must be an array ->build();
从PostType中移除功能支持
可以移除任何Wordpress核心功能。核心功能包括title
、editor
、comments
、revisions
、trackbacks
、author
、excerpt
、page-attributes
、thumbnail
、custom-fields
和post-formats
。
$custom_posttype = PostType::create('custom-posttype') ->removeSupport(['editor']) // Must be an array ->build();
设置PostType的slug
$custom_posttype = PostType::create('custom-posttype') ->setSlug('custom-slug') ->build();
在PostType中使用块编辑器
$custom_posttype = PostType::create('custom-posttype') ->addBlockEditor() ->build();
向PostType添加任何支持的选项
此库仅具有少量专用方法来设置文章类型选项。要使用其他文章类型选项,您可以使用addOption()
方法。请参阅可能的选项完整列表。
$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
此对象允许您轻松创建一个或多个自定义字段,而无需担心Wordpress钩子。链接任何您喜欢的链式方法,并以build()
方法注册自定义字段。
创建一个新的CustomField
$customfield = CustomField::create('new-customfield')->build();
设置CustomField类型
可能的CustomField类型包括text
、textarea
、checkbox
、number
、select
、radio
和editor
。
$new_customfield = CustomField::create('new-customfield') ->setType('textarea') ->build();
设置CustomField的标签
$new_customfield = CustomField::create('new-customfield') ->setLabel('New custom field') ->build();
向CustomField添加选项
如果CustomField是select
或radio
类型,则将使用这些选项。
$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();
设置CustomField的最小值
如果CustomField是number
类型,则将使用此值。
$new_customfield = CustomField::create('new-customfield') ->setType('number') ->setMin(0) ->build();
设置CustomField的最大值
如果CustomField是number
类型,则将使用此值。
$new_customfield = CustomField::create('new-customfield') ->setType('number') ->setMax(100) ->build();
设置CustomField的步长大小
如果CustomField是number
类型,则将使用此值。
$new_customfield = CustomField::create('new-customfield') ->setType('number') ->setStep(10) ->build();
MetaBox
此对象允许您轻松创建一个或多个元框,无需担心Wordpress钩子。您可以链式调用任何您喜欢的函数,并通过调用build()
方法来注册元框。
创建一个新的MetaBox
$custom_metabox = MetaBox::create('custom-metabox')->build();
设置MetaBox的标题
$custom_metabox = MetaBox::create('custom-metabox') ->setTitle('Metabox title') ->build();
向MetaBox添加自定义字段
$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();
向MetaBox添加列表
此库允许您轻松创建可增长和可重新排序的项目列表。列表中的每个项目都可以包含多个自定义字段。例如,如果您想向帖子中添加多个带有标题和描述的URL,可以使用列表来完成。
$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();
将MetaBox添加到Wordpress帖子类型
这些方法的参数必须是帖子类型的ID。这些方法允许您将MetaBox添加到由此库未创建的现有帖子类型。
$custom_metabox = MetaBox::create('custom-metabox') ->addPostType('page') // Or add multiple at once ->addPostTypes([ 'custom-posttype', 'another-posttype', ]) ->build();
自定义文本
此库在MetaBox中使用两个可自定义或翻译的文本字符串。以下文本被使用
new
新delete_confirm
您确定要删除此项目吗?
这就是如何自定义它们的方式
$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();
设置页面
此对象允许您轻松创建一个或多个设置页面,无需担心Wordpress钩子。您可以链式调用任何您喜欢的函数,并通过调用build()
方法来注册设置页面。
$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();