horttcore/wp-meta-box

This package is abandoned and no longer maintained. The author suggests using the ralfhortt/wp-meta-box package instead.

A WordPress meta box helper

3.0 2020-11-23 20:00 UTC

This package is auto-updated.

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


README

Installation

$ composer require ralfhortt/wp-meta-box

Usage

  • Extend RalfHortt\MetaBoxes\MetaBox()
  • You MUST set $this->identifier, $this->name, $this->screen in the class constructor
  • You CAN set the additional variables $this->context, $this->priority, $this->callbackArgs
  • You MUST Add a render() method
  • You CAN add a save() method
  • A nonce is added automatically and checked

Extend

Action

  • 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)

Example

<?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']));
	}
}

Changelog

3.0

  • Fix PSR4 namespace

2.0

  • Change namespace
  • Changed visibility of render and save form public to protected
  • Save method is now not mandatory
  • Added return type definition
  • Added action hooks
  • Validate context and priority
  • Fixed meta box callback arguments

1.1

  • Add save_post args

1.0

  • Initial release