Wordpress Metabox Class
<?php
/**
* Classe para criar Metaboxes mais manuteníveis no Wordpress.
*
* PHP versions 5.3+
*/
class Metabox
{
public function __construct($key, $title, $screen, $callback=null)
{
$this->key = $key;
$this->title = $title;
$this->screen = $screen;
$this->callback_function = $callback;
add_action('add_meta_boxes', array($this, 'metabox'));
add_action('save_post', array($this, 'save_metabox'));
}
public function metabox()
{
add_meta_box($this->key, $this->title, array($this, 'build_metabox'), $this->screen);
}
public function build_metabox($post)
{
$meta_val = get_post_meta($post->ID, $this->key);
call_user_func_array($this->callback_function, array($post, $this->key, $meta_val));
}
public function save_metabox($post_id)
{
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
update_post_meta($post_id, $this->key, $_POST[$this->key]);
}
};
/* Metabox de teste */
new Metabox(
'titulo',
'Título da minha Metabox',
null,
function($post, $meta_key, $meta_val) {
echo '<input type="text" value="'.esc_attr($meta_val[0]).'" name="'.$meta_key.'" />';
}
);