ralfhortt/wp-meta-box

WordPress 元盒助手

3.0 2020-11-23 20:00 UTC

This package is auto-updated.

Last update: 2024-09-29 00:21:59 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

扩展

操作

  • do_action("before-meta-box-{$this->identifier}", $post, $callbackArgs)
  • do_action("after-meta-box-{$this->identifier}", $post, $callbackArgs)
  • do_action("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 方法的可见性从 public 更改为 protected
  • 现在保存方法不是必需的
  • 添加了返回类型定义
  • 添加了动作钩子
  • 验证 contextpriority
  • 修复了元盒回调参数

1.1

  • 添加了 save_post 参数

1.0

  • 首次发布