moogatw / wp-metabox
为 WordPress 文章类型添加自定义字段。
Requires
- php: >=7.2
This package is auto-updated.
Last update: 2024-09-19 15:17:26 UTC
README
从 oberonlai/wp-metabox 派生的简单 WordPress 类用于 metabox 分支。
要求
安装
使用 composer 安装
在您的终端中运行以下命令以使用 Composer 安装。
$ composer require moogatw/wp-metabox
WP Metabox PSR-4 自动加载,并且可以使用 Composer 的自动加载器。以下是一个基本示例,尽管您的设置可能因您如何使用 Composer 而有所不同。
require __DIR__ . '/vendor/autoload.php'; use MGD\Metabox; $options = array( ... ); $books = new Metabox( $options );
有关与 Composer 和自动加载一起使用的基本指南,请参阅 Composer 的 基本用法 指南。
基本用法
以下是一个设置简单自定义筛选器的示例,该筛选器包含一个文章元字段。
// Require the Composer autoloader. require __DIR__ . '/vendor/autoload.php'; // Import PostTypes. use MGD\Metabox; ## Usage To create a metabox, first instantiate an instance of `Metabox`. The class takes one argument, which is an associative array. The keys to the array are similar to the arguments provided to the [add_meta_box](https://developer.wordpress.org/reference/functions/add_meta_box/) WordPress function; however, you don't provide `callback` or `callback_args`. ```php $metabox = new Metabox(array( 'id' => 'metabox_id', 'title' => 'My awesome metabox', 'screen' => 'post', // post type 'context' => 'advanced', // Options normal, side, advanced. 'priority' => 'default' ));
可用字段
在实例化上述 metabox 后,向其中添加一些字段。以下是一个可用字段的列表。
文本
一个简单的文本输入。没什么特别的。
$metabox->addText(array( 'id' => 'metabox_text_field', 'label' => 'Text', 'desc' => 'An example description paragraph that appears below the label.' ));
文本区域
文本区域用于存储文本正文。对于更丰富的 HTML 体验,请参阅 WYSIWYG 编辑器。
$metabox->addTextArea(array( 'id' => 'metabox_textarea_field', 'label' => 'Textarea', 'desc' => 'An example description paragraph that appears below the label.' ));
复选框
复选框是方便实现条件逻辑的好方法。
$metabox->addCheckbox(array( 'id' => 'metabox_checkbox_field', 'label' => 'Checkbox', 'desc' => 'An example description paragraph that appears below the label.' ));
单选按钮
单选按钮字段是选择选项集的好方法。
$metabox->addRadio( array( 'id' => 'metabox_radio_field', 'label' => 'Radio', 'desc' => 'An example description paragraph that appears below the label.', ), array( 'key1' => 'Value One', 'key2' => 'Value Two' ) );
选择
选择字段是选择选项集的好方法。
$metabox->addSelect( array( 'id' => 'metabox_select_field', 'label' => 'Select', 'desc' => 'An example description paragraph that appears below the label.', ), array( 'key1' => 'Value One', 'key2' => 'Value Two' ) );
图片上传
使用此功能允许用户在 metabox 中上传图片。提示:使用 repeater 与动态管理相册或幻灯片中照片。
$metabox->addImage(array( 'id' => 'metabox_image_field', 'label' => 'Image Upload', 'desc' => 'An example description paragraph that appears below the label.' ));
WYSIWYG 编辑器
您可以使用 WYSIWYG 编辑器来方便地管理 HTML 内容。
$metabox->addEditor(array( 'id' => 'metabox_editor_field', 'label' => 'Editor', 'desc' => 'An example description paragraph that appears below the label.' ));
数字
数字字段限制为输入数字。
$metabox->addNumber(array( 'id' => 'metabox_number_field', 'label' => 'Number', 'desc' => 'An example description paragraph that appears below the label.' ));
颜色
颜色字段用于选择颜色代码。
$metabox->addColor(array( 'id' => 'metabox_color_field', 'label' => 'Color', 'desc' => 'An example description paragraph that appears below the label.' ));
网格系统
新添加的网格系统用于字段宽度,使用 col-1 到 12,默认为 col-12。
$metabox->addText(array( 'id' => 'metabox_text_field', 'label' => 'Text', 'desc' => 'An example description paragraph that appears below the label.' 'col' => '6', )); $metabox->addRadio( array( 'id' => 'metabox_radio_field', 'label' => 'Radio', 'desc' => 'An example description paragraph that appears below the label.', 'col' => '3', ), array( 'key1' => 'Value One', 'key2' => 'Value Two' ) );
重复器
所有上述字段都可以添加到重复器中,以存储动态长度的内容数组。以下是一个具有三个字段(文本、文本区域和图片上传)的重复器块的示例。
注意: true 是重复器字段的第二个参数。这是必需的。此外,存储重复器块字段的变量 $metabox_repeater_block_fields[] 在变量名称的末尾有一个对括号 []。这是必需的。
$metabox_repeater_block_fields[] = $metabox->addText(array( 'id' => 'metabox_repeater_text_field', 'label' => 'Photo Title' ), true); $metabox_repeater_block_fields[] = $metabox->addTextArea(array( 'id' => 'metabox_repeater_textarea_field', 'label' => 'Photo Description' ), true); $metabox_repeater_block_fields[] = $metabox->addImage(array( 'id' => 'metabox_repeater_image_field', 'label' => 'Upload Photo' ), true); $metabox->addRepeaterBlock(array( 'id' => 'metabox_repeater_block', 'label' => 'Photo Gallery', 'fields' => $metabox_repeater_block_fields, 'desc' => 'Photos in a photo gallery.', 'single_label' => 'Photo' ));