Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on a client's site, creating a staff profile plugin. The issue is that they previously created the profiles as regular posts. I converted all of the staff posts into a custom post types.

In order to customize the post admin I followed Dipesh Kc's solution to customize the default editor. I wanted to use the existing content for the staff posts, but customize the editor, removing the media uploader and other options.

UPDATE: While I successfully was able to change the default editor settings, and the post content is being displayed in the editor. I hooked wp_update_post into the save_post hook, thinking that what I'm editing is the post_content in a new custom metabox. When I update the post, I basically get a fatal error, memory exhausted warning, though when I check the database, the post_content does get updated with my edits.

The relevant sections of my plugin code are included below based on gicolek's gist and also by looking at the WP codex page for 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');
            }


        }

Does anyone know why the post_content isn't updating? Any insights would be appreciated.

share|improve this question
2  
How do you try to save the content? –  toscho Jun 14 '13 at 1:02
    
Hmm, currently it's not plugged into any function. I inferred that it would just update when the post is saved. –  tnog Jun 14 '13 at 3:36
    
I've been looking at the Codex for the documentation for wp_update_post. Do I need to update the "bio" post content and update it with a hook to "save_posts"? –  tnog Jun 14 '13 at 3:38
    
You have to hook into save_post to save the data. WordPress doesn’t know automatically what to do. –  toscho Jun 14 '13 at 8:57
    
Thank you very much for your hints toscho. I did take a look at the save_post action, and looked up some more documentation. I think that what's going on is that I'm creating a new metabox and custom field initially populated by the post_content? And then I'm using update_post_meta in the save_bio_data function hoooked into save_post to finally save the data. Though this hasn't worked either. I updated my original question with my current working code. What am I doing wrong here? –  tnog Jun 14 '13 at 21:31
show 2 more comments

1 Answer

up vote 1 down vote accepted

After some searching and working off of some pointers from toscho, and some other helpful posts to avoid an infinite loop I managed to figure out a solution. I'll post the code below then briefly explain:

// 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'));


        }

My problem was taking existing posts and converting them into a custom post type (the client had used regular posts to create a staff directory). I wanted to modify the default editor, rename and simplify it while using the existing post_content.

Basically, I looked at several pre-existing solutions (linked above) and arrived at my own. The important consideration is that I was using post_content, though it was now edited through a custom metabox. To make edits, I needed to run updates through the wp_update_post function hooked into save_post.

However there are issues with an infinite loop being made, so then I needed to unhook and rehook the save_post action as made clear in the WP Codex. The final issue was just having the correct formatting for the remove_action and add_action hooks, taking into consideration that I was calling them within my custom class.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.