maheshwaghmare / wp-meta-fields
为任何WordPress主题或插件注册自定义字段。为开发者构建。
Requires
- php: >=5.3.0
- composer/installers: ~1.0
Requires (Dev)
- phpunit/phpunit: 5.7.16|6.*
- squizlabs/php_codesniffer: 3.*
- wp-coding-standards/wpcs: ^2.0
README
为WordPress主题和插件注册元字段。为WordPress开发者构建。
使用Composer
安装库
如果您还没有composer.json,则使用以下命令初始化它:composer init
。
如果您已经有了composer.json,则使用以下命令安装包。
composer require maheshwaghmare/wp-meta-fields
如何使用?
// Load files. require_once 'vendor/autoload.php'; // Add meta box "Example Meta Box" for the post type 'post' and 'page'. mf_add_meta_box( array( 'id' => 'example-meta-box', 'title' => __( 'Example Meta Box' ), 'screen' => array( 'post', 'page' ), 'context' => 'normal', 'priority' => 'default', 'fields' => array( 'prefix-1-text' => array( 'type' => 'text', 'title' => __( 'Text Field', 'textdomain' ), 'description' => __( 'Simple text field for demonstration purpose.', 'textdomain' ), 'hint' => __( 'This is the Text Field for storing the text data for demonstration purpose.', 'textdomain' ), 'default' => '', ), ) ));
在这里,我们为page
和post
类型添加了一个文本字段。
我们的文本字段元键是prefix-1-text
。
我们可以使用短代码如[mf meta_key="prefix-1-text"]
来获取元字段值。
我们可以注册许多其他输入字段,如text
、textarea
、password
、color
等。请参阅字段类型。
移除包
composer remove maheshwaghmare/wp-meta-fields --update-with-dependencies
如何添加到主题/插件中?
- 下载框架的最新zip文件,并将其解压到您的主题/插件中。
- 将以下代码添加到初始化框架。
require_once 'wp-meta-fields/wp-meta-fields.php';
注意:确保您有最新的wp-meta-fields
版本。从wp-meta-fields获取最新版本。
- 创建插件/主题中的
inc
目录。 - 将
WP Meta Fields
的最新版本解压到inc
目录中。 - 通过添加以下代码将框架包含到插件/主题中。
require_once 'inc/wp-meta-fields/wp-meta-fields.php';
注意:确保您有最新的wp-meta-fields
版本。从wp-meta-fields获取最新版本。
使用示例插件
要了解如何将元字段框架集成到插件中,请使用示例插件。
如何添加元框?
使用函数mf_add_meta_box()
注册元框及其元字段。它包含用于WordPress函数add_meta_box()的参数。
例如。
为Post
类型注册元框。
mf_add_meta_box( array( 'id' => 'example-all-fields', 'title' => __( 'Example - All Fields' ), 'screen' => array( 'post' ), 'context' => 'normal', 'priority' => 'default', 'fields' => array( // .. ) ));
其中,
如何添加字段?
注册一个具有唯一元键prefix-1-text
的单个text
字段,它位于上述已注册的元框中。
mf_add_meta_box( array( 'id' => 'example-meta-box', 'title' => __( 'Example Meta Box' ), 'screen' => array( 'post' ), 'context' => 'normal', 'priority' => 'default', 'fields' => array( 'prefix-1-text' => array( 'type' => 'text', 'title' => __( 'Text Field', 'textdomain' ), 'description' => __( 'Simple text field for demonstration purpose.', 'textdomain' ), 'hint' => __( 'This is the Text Field for storing the text data for demonstration purpose.', 'textdomain' ), 'default' => '', ), ) ));
这里,
上述已注册的字段在帖子编辑窗口中看起来如下截图。
如何打印/检索元字段值。
要检索/打印已注册字段prefix-1-text
的值,请使用
[mf meta_key='prefix-1-text'] or mf_meta( 'prefix-1-text' ); or echo mf_get_meta( 'prefix-1-text' );
- 使用短代码
[mf meta_key="META_KEY" post_id="POST_ID"]
来打印
元值。
例如:[mf meta_key='prefix-1-text']
默认情况下,它通过使用函数get_the_ID()
获取当前帖子ID。
或[mf meta_key='prefix-1-text' post_id='46']
通过传递帖子ID来指定帖子元值。
- 使用函数
mf_meta()
来打印
元值。
例如:<?php mf_meta( 'prefix-1-text' ); ?>
默认情况下,它通过使用函数get_the_ID()
获取当前帖子ID。
或<?php mf_meta( 'prefix-1-text', 46 ); ?>
通过传递帖子ID来指定帖子元值。
- 使用函数
mf_get_meta()
来检索
元值。
例如:<?php echo mf_get_meta( 'prefix-1-text' ); ?>
默认情况下,它通过使用函数get_the_ID()
获取当前帖子ID。
或<?php echo mf_get_meta( 'prefix-1-text', 46 ); ?>
通过传递帖子ID来指定帖子元值。例如。
字段类型
现在,框架支持以下内置HTML5字段。
- 文本
- 文本区域
- 密码
- 颜色
- 日期
- 日期时间本地
- 电子邮件
- 月份
- 数字
- 时间
- 周
- 网址
- 复选框
- 单选按钮
- 下拉选择
示例
所有元字段
/** * Meta Fields (Screen - Normal) */ mf_add_meta_box( array( 'id' => 'example-all-fields', 'title' => __( 'Example - All Fields' ), 'screen' => array( 'post' ), 'context' => 'normal', 'priority' => 'default', 'fields' => array( 'prefix-1-text' => array( 'type' => 'text', 'title' => 'Text Field', 'description' => 'Text Field field description goes here.', 'hint' => 'Text Field field description goes here.', 'default' => '', ), 'prefix-1-textarea' => array( 'type' => 'textarea', 'title' => 'Textarea', 'description' => 'Textarea field description goes here.', 'hint' => 'Textarea field description goes here.', 'default' => '', ), 'prefix-1-password' => array( 'type' => 'password', 'title' => 'Password', 'description' => 'Password field description goes here.', 'hint' => 'Password field description goes here.', 'default' => '', ), 'prefix-1-color' => array( 'type' => 'color', 'title' => 'Color', 'description' => 'Color field description goes here.', 'hint' => 'Color field description goes here.', 'default' => '#f3f3f3', ), 'prefix-1-date' => array( 'type' => 'date', 'title' => 'Date', 'description' => 'Date field description goes here.', 'hint' => 'Date field description goes here.', 'default' => '', ), 'prefix-1-datetime-local' => array( 'type' => 'datetime-local', 'title' => 'Date Time Local', 'description' => 'Date Time Local field description goes here.', 'hint' => 'Date Time Local field description goes here.', 'default' => '', ), 'prefix-1-email' => array( 'type' => 'email', 'title' => 'Email', 'description' => 'Email field description goes here.', 'hint' => 'Email field description goes here.', 'default' => '', ), 'prefix-1-month' => array( 'type' => 'month', 'title' => 'Month', 'description' => 'Month field description goes here.', 'hint' => 'Month field description goes here.', 'default' => '', ), 'prefix-1-number' => array( 'type' => 'number', 'title' => 'Number', 'description' => 'Number field description goes here.', 'hint' => 'Number field description goes here.', 'default' => '', ), 'prefix-1-time' => array( 'type' => 'time', 'title' => 'Time', 'description' => 'Time field description goes here.', 'hint' => 'Time field description goes here.', 'default' => '', ), 'prefix-1-week' => array( 'type' => 'week', 'title' => 'Week', 'description' => 'Week field description goes here.', 'hint' => 'Week field description goes here.', 'default' => '', ), 'prefix-1-url' => array( 'type' => 'url', 'title' => 'Url', 'description' => 'Url field description goes here.', 'hint' => 'Url field description goes here.', 'default' => '', ), 'prefix-1-checkbox' => array( 'type' => 'checkbox', 'title' => 'Checkbox', 'description' => 'Checkbox field description goes here.', 'hint' => 'Checkbox field description goes here.', 'default' => true, ), 'prefix-1-radio' => array( 'type' => 'radio', 'title' => 'Radio', 'description' => 'Radio field description goes here.', 'hint' => 'Radio field description goes here.', 'default' => 'one', 'choices' => array( 'one' => 'One', 'two' => 'Two', 'three' => 'Three', ), ), 'prefix-1-select' => array( 'type' => 'select', 'title' => 'Select', 'description' => 'Select field description goes here.', 'hint' => 'Select field description goes here.', 'default' => 'one', 'choices' => array( 'one' => 'One', 'two' => 'Two', 'three' => 'Three', ), ), ) ) );
它生成如下截图所示的元框和元字段。