自定义Metabox类中的SAVE_POST操作不起作用

时间:2013-02-28 作者:Danny

我正在编写一个类来创建元盒并保存表单数据。问题是,我在单击“发布”时不断收到警告:

警告:call_user_func_array() 要求参数1是有效的回调函数Mighty_Metabox 找不到或函数名无效“

我知道的一些事情:meta框显示在页面加载上。页面加载时调用save函数。发布时返回上述警告。发布时不调用save函数。

我需要把save_post 其他地方的动作钩?我把wordpress集成到我的课堂上是不是完全错了?对于如何以及何时调用save方法等方法的一些帮助,我们将不胜感激:

    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;
    }
}
以下是用法:

$args = array
(
    \'id\' => \'_subtitle\',
    \'title\' => \'Subtitle\',
    \'post_type\' => \'post\',
    \'context\' => \'normal\',
    \'priority\' => \'high\',
    \'template\' => \'subtitle.php\'
);

//subtitle metabox
$subtitle_mb = new Mighty_Metabox($args);
仅供参考:我对OOP编程相当陌生。我一直在看codex for metaboxes 为了了解更多信息,但他们的类似乎只是一些代码的包装器,似乎不允许您实例化它-我可能认为这是错误的。

EDIT: 这是副标题。包含的php文件:

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

1 个回复
SO网友:Vinod Dalvi

我已经修改了你的代码如下,它是为我工作。我已经从您的代码中删除了构造函数,并在方法中添加了代码,并使用object调用了该方法。

if (is_admin()){
add_action(\'load-post.php\',  array(\'Mighty_Metabox\' , \'Custom_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 Custom_Mighty_Metabox($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;
}
}

$args = array
(
\'id\' => \'_subtitle\',
\'title\' => \'Subtitle\',
\'post_type\' => \'post\',
\'context\' => \'normal\',
\'priority\' => \'high\',
\'template\' => \'subtitle.php\'
);

//subtitle metabox
$subtitle_mb = new Mighty_Metabox();
$subtitle_mb->Custom_Mighty_Metabox($args);

结束

相关推荐

WordPress中Single-{post-type}.php的固定链接结构

我尝试了多种方法将我的自定义帖子类型显示为single-{post-type} 最后,我用过之后,效果很好:\'rewrite\'=> false, 在那之前,我用\'rewrite\' => array( \'slug\' => \'custom-post-type\' ) 但对我来说从来都不管用。不知道我哪里出错了。现在我应该感到很高兴,因为它的工作没有任何错误。但permalink有一点困惑,因为它的显示:localhost/myblog/?news=break