devuri / cpt-meta-box
WordPress自定义文章类型的自定义元框和字段简单实现。
Requires
- php: ^7.1 || ^7.4 || ^8.0 || ^8.1
- devuri/wp-admin-page: ^3.3.2
Requires (Dev)
- 10up/phpcs-composer: dev-master
- friendsofphp/php-cs-fixer: ^3.0
- php-parallel-lint/php-parallel-lint: ^1.3
- phpstan/phpstan: ^1.8
- phpunit/phpunit: ^8.0|^9.0
- symfony/var-dumper: ^5.1.4
- szepeviktor/phpstan-wordpress: ^1.1
- vimeo/psalm: ^4.24
README
WordPress自定义文章类型的自定义元框和字段简单实现
这是一个PHP Composer包,可以帮助您创建WordPress元框和元字段。它还包括一个Data
类,您可以使用它来检索保存的元数据。
安装
要安装此包,请在您的终端中运行以下命令
composer require devuri/cpt-meta-box
元框和设置
这是一组PHP代码片段,用于使用MetaBox
和Settings
类创建WordPress元框及其相关设置。这些类提供了在WordPress中定义和处理自定义元框及其设置的功能。
元框类
MetaBox
类允许您在WordPress中创建自定义元框。它提供了以下功能
- 根据设置自动创建和渲染元框。
- 在保存文章时保存和更新元数据。
- 与
Settings
类集成以处理元框设置。
使用方法
要使用MetaBox
类,请按照以下步骤操作
- 在您的WordPress项目中包含此代码。
- 创建一个
Settings
子类的实例,例如提供的示例中的Details
类,传递相关的文章类型作为参数。 - 在您的
Settings
子类中实现settings()
方法来定义元框设置。此方法定义了元框中要显示的字段。 - 在您的
Settings
子类中实现data()
方法来处理从元框字段提交的数据。此方法在保存之前对数据进行清理和准备。 - 通过在
settings()
方法中添加字段并定义它们的设置来自定义settings()
和data()
方法。 - 创建一个
MetaBox
类的实例,传递Settings
对象和可选参数。这将自动在相关文章类型上创建和显示元框。 - 当更新文章时,将保存输入的数据。
设置类
Settings
抽象类为定义和处理与特定文章类型相关的设置提供了一个基础。它与MetaBox
类一起使用来定义元框的设置。
使用方法
要使用Settings
类,请按照以下步骤操作
- 创建一个扩展
Settings
类的子类。 - 实现
settings()
方法来定义元框设置。 - 实现
data()
方法来处理从元框字段提交的数据(请确保进行清理)。 - 通过在
settings()
方法中添加字段并定义它们的设置来自定义Settings
子类。 - 实例化
Settings
子类并将其传递给MetaBox
类构造函数以创建和显示元框。
示例使用
以下示例演示了MetaBox
和Settings
类的使用
use DevUri\PostTypeMeta\MetaBox; use DevUri\PostTypeMeta\Settings; // Implement meta fields class Details extends Settings { // The metabox settings public function settings(): void { // Basic input field echo self::form()->input('Title', $this->get_meta('title')); // Regular textarea echo self::form()->textarea('Description', $this->get_meta('description')); // Editor using a simplified version of wp_editor echo self::editor('Description', $this->get_meta('description')); } // The data, is $post_data $_POST and needs to be sanitized public function data($post_data): array { return [ 'title' => sanitize_textarea_field($post_data['title']), 'description' => sanitize_textarea_field($post_data['description_textarea']), ]; } } // Create a new instance of the Details class for the 'vehicle' post type $details = new Details('vehicle'); // Create a meta box without stripes new MetaBox($details); // Create a meta box with zebra table new MetaBox($details, true); // Create a meta box with NO zebra table new MetaBox($details, false); // Create a meta box with a custom label 'Vehicle Details' // and the meta key will be `vehicle-details_cpm` new MetaBox($details, ['name' => 'Vehicle Details']); // Create a meta box with a custom label 'Vehicle Details' and zebra stripes new MetaBox($details, [ 'name' => 'Vehicle Details', 'zebra' => true, ]); // Zebra styles are applied by default, this will also use zebra style new MetaBox($details, ['name' => 'Vehicle Details']); // Or instantiate directly, in this case the metabox will be `Details` based on the class name // and the meta key will be `details_cpm` new MetaBox(new Details('vehicle'));
此示例演示了如何使用MetaBox
和Settings
类为'vehicle'文章类型创建元框。此处的Details
类是Settings
的子类,并定义了元框设置和数据处理。使用MetaBox
类创建和显示元框,具有各种自定义选项。
数据类
Data
类提供各种实用方法来处理 WordPress 中的数据。它包括检索和操作与帖子相关的数据的功能,例如帖子元数据、帖子项以及生成自定义编辑链接。
使用方法
要使用 Data
类,请按照以下步骤操作
- 创建
Data
类的实例,可选地传递一个帖子类型作为参数。如果没有指定帖子类型,则默认使用 'post' 帖子类型。 - 利用
Data
类的可用方法执行数据相关操作。
方法
Data
类提供以下方法
__construct($post_type = null)
:使用可选的帖子类型参数初始化Data
对象。如果没有提供帖子类型,则使用默认的帖子类型 'post'。init($post_type)
:静态方法,用于创建具有指定帖子类型的Data
类的新实例。返回一个Data
对象。edit(int $id, $class = '')
:为给定的帖子 ID 生成编辑链接。返回作为字符串的编辑链接 HTML。此方法在生成链接之前检查当前用户是否有编辑帖子的权限。list()
:检索帖子项的列表,作为键值对。键是帖子 ID,值是帖子标题。返回一个数组。getkey($key, $data)
:通过其键从数组中检索值。如果键未设置,则返回null
。如果数据不是数组,则返回false
。meta($ID, $name = null)
:检索给定帖子 ID 的帖子元数据。使用指定的元名称检索元数据,如果没有提供,则使用帖子类型作为元名称。返回包含帖子 ID 和缩略图 ID 的元数据数组。items($n = -1)
:检索最新帖子或符合给定标准的帖子数组。可以通过$n
参数指定要检索的帖子数量。返回帖子对象或帖子 ID 的数组。
示例使用
以下是如何利用 Data
类的示例
use DevUri\PostTypeMeta\Data; // Create a new instance of the Data class for the 'vehicle' post type $data = Data::init('vehicle'); // Get a list of post items $postItems = $data->list(); // If you are not using the post type name as meta name you need to pass in the name in this example `details_cpm`: $metaData = $data->meta(123, 'details_cpm); // Retrieve the meta data for a specific post $metaData = $data->meta(123); // Generate an edit link for a post $editLink = Data::edit(123); // Get a value from an array by its key $value = Data::getkey('key_name', $dataArray); // Retrieve the latest 10 posts $latestPosts = $data->items(10); // Retrieve the latest 10 posts, // you can pass in array of arguments to filter retrieved posts. // See WP_Query::parse_query() for all available arguments. $latestPosts = $data->items(10, [ 'orderby' => 'date' ]); // if no arguments are provided these will be used. $defaults = [ 'numberposts' => 5, 'category' => 0, 'orderby' => 'date', 'order' => 'DESC', 'include' => array(), 'exclude' => array(), 'meta_key' => '', 'meta_value' => '', 'post_type' => 'vehicle', // based on the current Data class. 'suppress_filters' => true, ];
在这个示例中,我们为 'vehicle' 帖子类型创建了一个 Data
对象,并使用其方法检索帖子项、帖子元数据、生成编辑链接、从数组中获取值以及检索最新帖子。您可以根据自己的需求调整这些示例。
结论
此包提供了一种简单易用的方式来创建 WordPress 中的 MetaBoxes 和元字段。如果您有任何问题或问题,请随时提交问题。