devuri/cpt-meta-box

WordPress自定义文章类型的自定义元框和字段简单实现。

v0.4.2 2024-03-20 22:34 UTC

This package is auto-updated.

Last update: 2024-09-20 23:44:06 UTC


README

WordPress自定义文章类型的自定义元框和字段简单实现

这是一个PHP Composer包,可以帮助您创建WordPress元框和元字段。它还包括一个Data类,您可以使用它来检索保存的元数据。

安装

要安装此包,请在您的终端中运行以下命令

composer require devuri/cpt-meta-box

元框和设置

这是一组PHP代码片段,用于使用MetaBoxSettings类创建WordPress元框及其相关设置。这些类提供了在WordPress中定义和处理自定义元框及其设置的功能。

元框类

MetaBox类允许您在WordPress中创建自定义元框。它提供了以下功能

  • 根据设置自动创建和渲染元框。
  • 在保存文章时保存和更新元数据。
  • Settings类集成以处理元框设置。

使用方法

要使用MetaBox类,请按照以下步骤操作

  1. 在您的WordPress项目中包含此代码。
  2. 创建一个Settings子类的实例,例如提供的示例中的Details类,传递相关的文章类型作为参数。
  3. 在您的Settings子类中实现settings()方法来定义元框设置。此方法定义了元框中要显示的字段。
  4. 在您的Settings子类中实现data()方法来处理从元框字段提交的数据。此方法在保存之前对数据进行清理和准备。
  5. 通过在settings()方法中添加字段并定义它们的设置来自定义settings()data()方法。
  6. 创建一个MetaBox类的实例,传递Settings对象和可选参数。这将自动在相关文章类型上创建和显示元框。
  7. 当更新文章时,将保存输入的数据。

设置类

Settings抽象类为定义和处理与特定文章类型相关的设置提供了一个基础。它与MetaBox类一起使用来定义元框的设置。

使用方法

要使用Settings类,请按照以下步骤操作

  1. 创建一个扩展Settings类的子类。
  2. 实现settings()方法来定义元框设置。
  3. 实现data()方法来处理从元框字段提交的数据(请确保进行清理)。
  4. 通过在settings()方法中添加字段并定义它们的设置来自定义Settings子类。
  5. 实例化Settings子类并将其传递给MetaBox类构造函数以创建和显示元框。

示例使用

以下示例演示了MetaBoxSettings类的使用

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'));

此示例演示了如何使用MetaBoxSettings类为'vehicle'文章类型创建元框。此处的Details类是Settings的子类,并定义了元框设置和数据处理。使用MetaBox类创建和显示元框,具有各种自定义选项。

数据类

Data 类提供各种实用方法来处理 WordPress 中的数据。它包括检索和操作与帖子相关的数据的功能,例如帖子元数据、帖子项以及生成自定义编辑链接。

使用方法

要使用 Data 类,请按照以下步骤操作

  1. 创建 Data 类的实例,可选地传递一个帖子类型作为参数。如果没有指定帖子类型,则默认使用 'post' 帖子类型。
  2. 利用 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 和元字段。如果您有任何问题或问题,请随时提交问题。