ralfhortt / wp-meta-box
A WordPress meta box helper
Installs: 168
Dependents: 4
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
pkg:composer/ralfhortt/wp-meta-box
This package is auto-updated.
Last update: 2025-10-29 02:43:56 UTC
README
Installation
$ composer require ralfhortt/wp-meta-box
Usage
- Extend
RalfHortt\MetaBoxes\MetaBox() - You MUST set
$this->identifier,$this->name,$this->screenin 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
renderandsaveform public to protected - Save method is now not mandatory
- Added return type definition
- Added action hooks
- Validate
contextandpriority - Fixed meta box callback arguments
1.1
- Add save_post args
1.0
- Initial release