horttcore/wp-meta-box

此包已被废弃,不再维护。作者建议使用 ralfhortt/wp-meta-box 包。

WordPress 元数据框助手

3.0 2020-11-23 20:00 UTC

This package is auto-updated.

Last update: 2022-03-28 19:22:55 UTC


README

安装

$ composer require ralfhortt/wp-meta-box

用法

  • 扩展 RalfHortt\MetaBoxes\MetaBox()
  • 您必须在类构造函数中设置 $this->identifier$this->name$this->screen
  • 您还可以设置其他变量 $this->context$this->priority$this->callbackArgs
  • 您必须添加一个 render() 方法
  • 您还可以添加一个 save() 方法
  • 自动添加并检查 nonce

扩展

操作

  • 执行操作 "before-meta-box-{$this->identifier}", $post, $callbackArgs
  • 执行操作 "after-meta-box-{$this->identifier}", $post, $callbackArgs
  • 执行操作 "saved-meta-box-{$this->identifier}", $postId, $post, $update

示例

<?php
use RalfHortt\MetaBoxes\MetaBox;

class MyMetaBox extends MetaBox
{
	
	public function __construct()
	{
		$this->identifier = 'my-meta-box';
		$this->name = __('My Meta Box', 'textdomain');
		$this->screen = ['post'];
		$this->context = 'side';
		$this->priority = 'high';
	}

	protected function render(\WP_Post $post, array $callbackArgs): void
	{
		?>
		<label for="my-meta">Meta Label</label>
		<input id="my-meta" name="my-meta" class="regular-text" type="text" value="<?= esc_attr(get_post_meta($post->ID, 'my-meta', true )) ?>">
		<?php
	}

	protected function save(int $postId, \WP_Post $post, bool $update): void
	{
		update_post_meta($postId, 'my-meta', sanitize_text_field($_POST['my-meta']));
	}
}

变更日志

3.0

  • 修复 PSR4 命名空间

2.0

  • 更改命名空间
  • rendersave 的可见性从公共更改为受保护
  • 保存方法现在不是必需的
  • 添加返回类型定义
  • 添加操作挂钩
  • 验证 contextpriority
  • 修复元框回调参数

1.1

  • 添加 save_post 参数

1.0

  • 首次发布