插件代码在类中不能正常工作

时间:2013-03-11 作者:Nathan

我编写了一段简单的代码(从网上的各种教程中拼凑而成),它创建了一个简单的元框(称为“副标题”),并从管理区域中删除了自定义字段。

在我将代码放入类之前,一切都很好。一旦我这样做,元框就会停止保存到数据库。我怀疑这是因为我需要为构造函数定义一些参数,但我尝试的任何操作都会失败。我不知道出了什么问题,所以非常感谢您的帮助!代码如下:

class MySubBoxClass 
{  
    function __construct()
    {
        add_action( \'add_meta_boxes\', array( $this, \'meta_box_add\' ) );      //Hooks meta_box_add() onto the add_meta_boxes hook                                                                
        add_action( \'save_post\', array( $this, \'save_meta_box\', 10, 2 ) );   //Hooks save_meta_box() onto the save_post hook                                                                          
        add_action(\'admin_init\', array( $this, \'remove_custom_meta_boxes\') );//Hooks remove_custom_meta_boxes() onto admin_init  
    }

    public function meta_box_add()    //Function that triggers the add_meta_box() Wordpress function 
    {
        add_meta_box( \'meta-box-id\', \'SUBHEADING\', array($this, \'html_form_func\'), \'post\', \'normal\', \'high\' );  
        //Adds meta box with HTML id "meta-box" to the admin panel in the edit posts screen  
        //Calls the add_meta_box_cb_function() which creates the HTML form (see below)  
    }


    public function html_form_func( $object ) //Creates the HTML form and outputs its value if it has one   
    {  
        wp_nonce_field( basename( __FILE__ ), \'meta_box_nonce\' );

        <label><input type="text" name="subheading" id="meta-box-input" size="144" value="<?php echo get_post_meta( $object->ID, \'subheading\', true ); ?>" /></label>        
    }

    public function save_meta_box( $post_id, $post ) 
    {
        //Verifies the nonce before proceeding.
        if ( !isset( $_POST[\'meta_box_nonce\'] ) || !wp_verify_nonce( $_POST[\'meta_box_nonce\'], basename( __FILE__ ) ) )
            return $post_id;

        //Gets the post type object
        $post_type = get_post_type_object( $post->post_type );


        //Assigns \'$new_meta_value\' variable the value that was newly submitted through the HTML form input named \'subheading\' 
        $new_meta_value = ( isset( $_POST[\'subheading\'] ) ? esc_attr( $_POST[\'subheading\'] ) : \'\' );

        //Assigns\'$meta_key\' variable the value \'subheading\'  
        $meta_key = \'subheading\';

        //Assigns $meta_value variable the value of the custom field (identified by the key \'$meta_key\') that is currently 
        //associated with that post 
        $meta_value = get_post_meta( $post_id, $meta_key, true );

        //If a new meta value was added and the previous meta value had no value (== \'\'), add the new meta value
        if ( $new_meta_value && $meta_value == \'\' )
            add_post_meta( $post_id, $meta_key, $new_meta_value, true );

        //If the new meta value does not match the old value, update it
        elseif ( $new_meta_value && $new_meta_value != $meta_value )
            update_post_meta( $post_id, $meta_key, $new_meta_value );

        //If there is no new meta value but an old value exists, delete it
        //elseif ( $new_meta_value == \'\' && $meta_value )
        //delete_post_meta( $post_id, $meta_key, $meta_value );
    }

    public function remove_custom_meta_boxes() 
    {
        remove_meta_box(\'postcustom\',\'post\',\'normal\');
        remove_meta_box(\'postcustom\',\'page\',\'normal\');
    }
}

$MySubBoxClass = new MySubBoxClass;

1 个回复
最合适的回答,由SO网友:Mike Madern 整理而成

启用神奇方法\\uu构造

您必须更改以下行

$MySubBoxClass = new MySubBoxClass;
进入

$MySubBoxClass = new MySubBoxClass();
这样__construct() 将使用魔法方法
Theadd_action 现在不调用方法。

中的错误html_form_func( $object )

更改

public function html_form_func( $object ) //Creates the HTML form and outputs its value if it has one   
{  
    wp_nonce_field( basename( __FILE__ ), \'meta_box_nonce\' );

    <label><input type="text" name="subheading" id="meta-box-input" size="144" value="<?php echo get_post_meta( $object->ID, \'subheading\', true ); ?>" /></label>        
}
进入

public function html_form_func( $object ) //Creates the HTML form and outputs its value if it has one   
{  
    wp_nonce_field( basename( __FILE__ ), \'meta_box_nonce\' );
    ?>
    <label><input type="text" name="subheading" id="meta-box-input" size="144" value="<?php echo get_post_meta( $object->ID, \'subheading\', true ); ?>" /></label>
    <?php
}
参照对象方法更改

function __construct()
  {
  add_action( \'add_meta_boxes\', array( $this, \'meta_box_add\' ) );      //Hooks meta_box_add() onto the add_meta_boxes hook                                                                

  add_action( \'save_post\', array( $this, \'save_meta_box\', 10, 2 ) );   //Hooks save_meta_box() onto the save_post hook                                                                          

  add_action(\'admin_init\', array( $this, \'remove_custom_meta_boxes\') );//Hooks remove_custom_meta_boxes() onto admin_init  
  }
进入

function __construct()
{
    add_action( \'add_meta_boxes\', array( &$this, \'meta_box_add\' ) );        //Hooks meta_box_add() onto the add_meta_boxes hook                                                                
    add_action( \'save_post\', array( &$this, \'save_meta_box\', 10, 2 ) );     //Hooks save_meta_box() onto the save_post hook                                                                          
    add_action( \'admin_init\', array( &$this, \'remove_custom_meta_boxes\' ) );//Hooks remove_custom_meta_boxes() onto admin_init  
}
如果在中使用对象方法add_action 你必须使用&$this 使用$this 参考中。

结束

相关推荐

Travel Blog Plugins

今年晚些时候,我将使用Wordpress创建一个关于我旅行的博客。我希望该博客具有以下功能我的帖子将被地理定位一张包含帖子位置的地图,可以单击地图上的各个点到达帖子</我正在寻找最好/最合适的插件。谢谢,艾尔。