自定义wp_EDITOR不更新POST_CONTENT

时间:2013-06-13 作者:tnog

我正在一个客户的网站上工作,创建一个员工档案插件。问题是,他们之前创建的配置文件是常规帖子。我将所有员工的职位转换为自定义的职位类型。

为了自定义我遵循的post adminDipesh Kc\'s solution 自定义默认编辑器。我想为员工帖子使用现有内容,但要自定义编辑器,删除媒体上传器和其他选项。

更新:虽然我成功地更改了默认编辑器设置,但帖子内容正在编辑器中显示。我上钩了wp_update_post 进入save_post hook认为我正在编辑的是post_content 在新的自定义元数据库中。当我更新帖子时,我基本上得到了一个致命错误,内存耗尽警告,尽管当我检查数据库时post_content 使用我的编辑进行更新。

我的插件代码的相关部分包含在以下基于gicolek\'s gist 也可以通过查看WP codex页面wp_update_post:

public function __construct() {
        add_action( \'add_meta_boxes\', array($this, \'meta_boxes\' ));
        add_action( \'save_post\', array($this, \'save_bio_data\' ), 10, 1);
    }


    // Modify default content editor

    /**
     * Adds a box to the main column on the Post edit screen uses modified editor
     * 
     * @hook add_meta_boxes
     * @see http://codex.wordpress.org/Function_Reference/add_meta_box 
     */
     public function meta_boxes()
    {
        global $_wp_post_type_features;      
            //check for the required post type page or post or <custom post type(here article)  
            if (isset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\'])) {
                unset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\']);
                add_meta_box(
                    \'tn_cstm_people_bio\',
                    __(\'Bio\'),
                    // To address callback function in a class
                    array($this, \'bio_editor_meta_box\'),
                    \'tn_cstm_people\', \'normal\', \'core\'
                );
            }
    }

    public function bio_editor_meta_box($post)
        {   
            // Use nonce for verification
            wp_nonce_field( plugin_basename( __FILE__ ), \'tn_cstm_noncename\' );


            $settings = array(
                \'media_buttons\' => false,
                \'textarea_name\'=>\'tn_cstm_bio\',
                \'tabindex\' => \'4\',
                \'teeny\' => true
            );

            wp_editor($post->post_content,\'tn_cstm_bio\', $settings);
    }



    /**
     * When the post is saved, save our customized post data 
     * 
     * @hook save_post
     */
    public function save_bio_data($post_id) {

        // check if we\'re on the cpt page and if the nonce has been set
        if ( !isset( $_POST[\'post_type\'] ) or !isset( $_POST[\'tn_cstm_noncename\'] ) ) {
            return;
        }

        // check if the we\'re on the custom post page
        if ( !in_array( $_POST[\'post_type\'], array( \'tn_cstm_people\' ) ) ) {
            return;
        }

        // if we\'re doing an auto save return
        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return;

        // verify nonce for the safety reasones
        // @see http://codex.wordpress.org/WordPress_Nonces
        if ( !wp_verify_nonce( $_POST[\'tn_cstm_noncename\'], plugin_basename( __FILE__ ) ) )
            return;

        /* Make sure that current user has proper rights to save the post
         *@see Caution! in http://codex.wordpress.org/Function_Reference/wp_update_post 
          *Otherwise would generate multiple (hundreds of copies of the post)
         */

        if ( !current_user_can( \'edit_post\', $post_id ) )
            return;


              /* Verify that post type is not set to revision and that $post object needs to be updated
                *http://codex.wordpress.org/Function_Reference/wp_update_post
                */

             if ( ! wp_is_post_revision( $post_id ) ) {

                // unhook this function so it doesn\'t loop infinitely
                remove_action(\'save_post\', \'save_bio_data\');

                // update the post, which calls save_post again

                $data_content = $_POST[\'tn_cstm_bio\'];
                $my_post = array();
                $my_post[\'ID\'] = $post_id;
                $my_post[\'post_content\'] = $data_content;

                wp_update_post( $my_post );

                // re-hook this function
                add_action(\'save_post\', \'save_bio_data\');
            }


        }
有人知道post\\u内容没有更新的原因吗?如有任何见解,将不胜感激。

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

在对toscho的一些指针进行一些搜索和处理之后some other helpful posts to avoid an infinite loop 我设法想出了一个解决办法。我将在下面发布代码,然后简要解释:

// Hook into the actions here
    public function __construct() {
        add_action( \'add_meta_boxes\', array($this, \'meta_boxes\' ));
        add_action( \'save_post\', array($this, \'save_bio_data\' ), 10, 1);
    }



  // Modify default content editor

    /**
     * Adds a box to the main column on the Post edit screen uses modified editor
     * 
     * @hook add_meta_boxes
     * @see http://codex.wordpress.org/Function_Reference/add_meta_box 
     */
     public function meta_boxes()
    {
        global $_wp_post_type_features;      
            //check for the required post type page or post or <custom post type(here article)  
            if (isset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\'])) {
                unset($_wp_post_type_features[\'tn_cstm_people\'][\'editor\']);
                add_meta_box(
                    \'tn_cstm_people_bio\',
                    __(\'Bio\'),
                    // To address callback function in a class
                    array($this, \'bio_editor_meta_box\'),
                    \'tn_cstm_people\', \'normal\', \'core\'
                );
            }
    }

    public function bio_editor_meta_box($post)
        {   
            // Use nonce for verification
            wp_nonce_field( plugin_basename( __FILE__ ), \'tn_cstm_noncename\' );


            $settings = array(
                // Removed media upload buttons and added other values here
                \'media_buttons\' => false,
                \'textarea_name\'=>\'tn_cstm_bio\',
                \'tabindex\' => \'4\',
                \'teeny\' => true
            );
            // Define your custom wp_editor here. Use existing post content: $post->post_content
            wp_editor($post->post_content,\'tn_cstm_bio\', $settings);
    }



    /**
     * When the post is saved, save our customized post data 
     * 
     * @hook save_post
     */
    public function save_bio_data($post_id) {

        // check if we\'re on the post page and if the nonce has been set
        if ( !isset( $_POST[\'post_type\'] ) or !isset( $_POST[\'tn_cstm_noncename\'] ) ) {
            return;
        }

        // check if the we\'re on the custom post page
        if ( !in_array( $_POST[\'post_type\'], array( \'tn_cstm_people\' ) ) ) {
            return;
        }

        // if we\'re doing an auto save return
        if ( defined( \'DOING_AUTOSAVE\' ) && DOING_AUTOSAVE )
            return;

        // verify nonce for the safety reasones
        // @see http://codex.wordpress.org/WordPress_Nonces
        if ( !wp_verify_nonce( $_POST[\'tn_cstm_noncename\'], plugin_basename( __FILE__ ) ) )
            return;

        // make sure that current user has proper rights to save the post
        // @see http://codex.wordpress.org/Roles_and_Capabilities
        if ( !current_user_can( \'edit_post\', $post_id ) )
            return;


              /* Verify that post type is not set to revision and that $post object needs to be updated
                *http://codex.wordpress.org/Function_Reference/wp_update_post
                */

                // unhook this function so it doesn\'t loop infinitely
                remove_action(\'save_post\', array($this,\'save_bio_data\'));

                // update the post, which calls save_post again

                $data_content = $_POST[\'tn_cstm_bio\'];
                $my_post = array();
                $my_post[\'ID\'] = $post_id;
                $my_post[\'post_content\'] = $data_content;

                wp_update_post( $my_post );

                // re-hook this function don\'t forget to format as add_action(\'save_post\', array($this, \'$function_name\'))\'
                add_action(\'save_post\', array($this, \'save_bio_data\'));


        }
我的问题是获取现有帖子并将其转换为自定义帖子类型(客户使用常规帖子创建员工目录)。我想修改默认编辑器,重命名并简化它,同时使用现有的post\\u内容。

基本上,我查看了几个预先存在的解决方案(如上所述),并得出了自己的解决方案。重要的考虑是我使用post_content, 虽然它现在是通过自定义元数据库编辑的。要进行编辑,我需要通过wp_update_post 连接到的函数save_post.

然而,在进行无限循环时存在一些问题,因此我需要取消挂钩并重新挂钩save_post 行动如WP Codex. 最后一个问题是对remove_actionadd_action hooks,考虑到我在自定义类中调用它们。

结束

相关推荐