Wordpress Plugin Tutorial Part V - Saving The Data

We created a custom meta box on part four of this tutorial series. Today we will try to store the custom comment message and associate this message to a post. To do this we will have to hook into the 'save_post' action and provide a function that will save the data.
First we will look at the function that will save the data to the database. You have to place this inside the "CustomCommentMessage" class.
public function ccm_save_data( $post_id )
{
    //If this is an auto save we will not do anything
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
        return;
    }
    //If this a post (and not a page) and 
    //if the user has privilages
    else if ( 'post' == $_POST['post_type'] && 
            current_user_can( 'edit_page', $post_id ) )
    {
        $ccm = $_POST['ccm_title_reply'];
        //add the value to database if it is not there
        add_post_meta($post_id, 'ccm_title_reply', $ccm, true)
                or
        //update it if the value is already there
        update_post_meta($post_id, 'ccm_title_reply', $ccm);
    }
}
You can read the comments and easily understand what the code does. The third parameter of the add_post_meta function specifies if the meta field is unique. In other words, the meta value should not take more than one value. Since we only need one "comment message" per post we set this to "ture". If the value is already there, the add_post_meta function will return false. In such a case we will simply update the value.

Now we have to hook this function into the 'save_post' action. To do this, change the constructor of the class to the following.
public function __construct()
{
    add_action( 'add_meta_boxes',
                array( &$this, 'add_ccm_meta_box' ) );

    add_action('save_post', 
                array(&$this,'ccm_save_data') ) ;
}
We have simply added a new action hook in the constructor.
Now when the post is saved, it will be saved to the database, but it will not be shown on the "Edit Post" screen. In order to show this on the "Edit Post" screen,  change the function "render_ccm_meta_box_content" to look like below.
public function render_ccm_meta_box_content( $post )
{            
?>
    <label for="ccm__title_reply">
         Custom Comment Message Title : 
    </label>
    <input type="text"
           id="ccm_title_reply"
           name="ccm_title_reply"
           value="<?php echo get_post_meta($post->ID,'ccm_title_reply',true); ?>"
           size="25" />
<?php
}

At the end the "custom-comment-message.php" file will look like this.
<?php
/* 
Plugin Name: Custom Comment Message
*/


if (!class_exists("CustomCommentMessage")) {
    class CustomCommentMessage {
        public function __construct()
        {
            add_action( 'add_meta_boxes',
                        array( &$this, 'add_ccm_meta_box' ) );

            add_action('save_post', 
                        array(&$this,'ccm_save_data') ) ;
        }

        public function add_ccm_meta_box()
        {
            add_meta_box(
                 'ccm_meta_box'
                ,'Custom Comment Message'
                ,array( &$this, 'render_ccm_meta_box_content' )
                ,'post'                
            );
        }

        public function render_ccm_meta_box_content( $post )
        {            
?>
            <label for="ccm__title_reply">
                 Custom Comment Message Title : 
            </label>
            <input type="text"
                   id="ccm_title_reply"
                   name="ccm_title_reply"
                   value="<?php echo get_post_meta($post->ID,
                                                   'ccm_title_reply',
                                                   true); ?>"
                   size="25" />
<?php
        }

        public function ccm_save_data( $post_id )
        {
            if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ){
                return;
            }
            else if ( 'post' == $_POST['post_type'] && 
                    current_user_can( 'edit_page', $post_id ) )
            {
                $ccm = $_POST['ccm_title_reply'];
                add_post_meta($post_id, 'ccm_title_reply', $ccm, true)
                        or
                update_post_meta($post_id, 'ccm_title_reply', $ccm);
            }
        }
    }
}

if (class_exists("CustomCommentMessage")) {
    $ccmessage = new CustomCommentMessage();
}

?>
A few things to note here is that the '$post_id' variable is automatically passed to the 'ccm_save_data' function. You can refer this page to see a description about the 'save_post' action hook. Next we will discuss how this functionality can be made more secure by using a nonce.

No comments:

Post a Comment