I am writing a class to create metaboxes and save the form data. The problem is, I keep getting a warning when I click "publish":
Warning:
call_user_func_array()
expects parameter 1 to be a valid callback, functionMighty_Metabox
not found or invalid function name"
Some of the things I know: The meta box shows on page load. The save function is called on page load. The warning above is returned on publish. The save function is not called on publish.
Do I need to put the save_post
action hook somewhere else? Am I getting the wordpress integration into my class completely wrong? Some help on how and when to call methods like the save method would be greatly appreciated:
if (is_admin()){
add_action('load-post.php', 'Mighty_Metabox');
}
//the class
class Mighty_Metabox{
//the vars
public $id = false;
public $title = false;
public $callback = array();
public $post_type = array();
public $context = false;
public $priority = false;
public $callback_args = array();
public $template = false;
public function __construct($params=false){
if($params){
//arrange params as key => value pairs
foreach($params as $key => $value){
$this->{$key} = $value;
}
//if admin page, add the action - add (metabox)
if(is_admin){
//set the callback to create metabox
$this->callback = array($this, '_create');
//add the add metabox action
add_action('add_meta_boxes', array($this, '_add'));
//add the save metabox action
add_action('save_post', array($this,'_save'));
}
}
}
function _add(){
//add the metabox with user set params
add_meta_box($this->id, $this->title, $this->callback, $this->post_type, $this->context, $this->priority, $this->callback_args);
}
function _create(){
//create the metabox
include($this->template);
// create a nonce for verification
echo '<input type="hidden" name="'. $this->id .'_nonce" value="' . wp_create_nonce($this->id) . '" />';
}
function _save(){
// verify if this is an auto save routine.
// If it is our form has not been submitted, so we dont want to do anything
if (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) {
return;
}
// verify this came from the our screen and with proper authorization,
// because save_post can be triggered at other times
$nonce = isset($_POST[$this->id.'_nonce']) ? $_POST[$this->id.'_nonce'] : NULL;
if (!wp_verify_nonce($nonce, $this->id)){
return $post_id;
}
$this->_debug(false); // won't get here on page load
}
private function _debug($exit=true){
var_dump(debug_backtrace());
return ($exit)? exit(): false;
}
}
And here is the use:
$args = array
(
'id' => '_subtitle',
'title' => 'Subtitle',
'post_type' => 'post',
'context' => 'normal',
'priority' => 'high',
'template' => 'subtitle.php'
);
//subtitle metabox
$subtitle_mb = new Mighty_Metabox($args);
FYI: I am fairly new to OOP programming. I have been looking at the codex for metaboxes in order to learn more, but their class just seems like a wrapper for some code and doesn't seem to allow you to instantiate it - I may be wrong in thinking this.
EDIT: Here is the subtitle.php file that is being included:
<div class="my_meta_control">
<table class='metabox'>
<tr>
<th valign='top'><label>Subtitle</label></th>
<td>
<input type="text" id="<?php echo $this->id; ?>" name="<?php echo $this->id; ?>" value="" class='large-text' /><br />
<span class='description'>The subtitle will appear under the main post or page title.</span>
</td>
</tr>
</table>