As of WordPress 3.3, the Edit Comments Screen allows us to change the name, email, url, status, and text of a blog comment. If we are using the Akismet spam filter, then there will also be a Comment History metabox.
Here, we describe how we can expand the Edit Comments Screen by adding a new metabox.
For example, in the screenshot above, we added an Extra Arguments metabox to the Edit Comments screen. It allows us to change the –
- Comment Post – By changing the post ID that is associated with the comment, we can move the comment from one post or page to any other post of our choice. In fact, we could move it from any post type (e.g. post, page, custom post type) to another.
- Comment Parent – Changing this value allows us to thread a comment, or remove the comment from a thread (by setting its parent to 0).
- Author IP – Change the comment IP.
1. Create and Add Metabox
We start by creating our metabox input elements and then adding it using the WordPress add_meta_box function.
// Add to the admin_init function add_meta_box('shiba_comment_xtra_box', __('Extra Arguments'), 'my_metabox', 'comment', 'normal');
Note – As of WordPress 3.3 new comment metaboxes can only be added to the normal context. I.e., argument 5 must be set to ‘normal’ for our metabox to be rendered.
In addition to adding our metabox, we must also create it in our my_metabox function (argument 3). The metabox elements below were created by adapting the existing Author metabox, at the top of the Edit Comments screen. This code can be found in the wp-admin/edit-form-comment.php file.
function my_metabox($comment) { ?> <table class="form-table editcomment comment_xtra"> <tbody> <tr valign="top"> <td class="first"><?php _e( 'Comment Post:' ); ?></td> <td><input type="text" id="shiba_comment_post_ID" name="shiba_comment_post_ID" size="10" class="code" value="<?php echo esc_attr($comment->comment_post_ID); ?>" tabindex="1" /></td> </tr> <tr valign="top"> <td class="first"><?php _e( 'Comment Parent:' ); ?></td> <td><input type="text" id="shiba_comment_parent" name="shiba_comment_parent" size="10" class="code" value="<?php echo esc_attr($comment->comment_parent); ?>" tabindex="1" /></td> </tr> <tr valign="top"> <td class="first"><?php _e( 'Author IP:' ); ?></td> <td><input type="text" id="shiba_comment_author_IP" name="shiba_comment_author_IP" size="20" class="code" value="<?php echo esc_attr($comment->comment_author_IP); ?>" tabindex="1" /></td> </tr> </tbody> </table> <?php }
The code above creates three new text inputs for our metabox, populates it with the relevant values, and adds it to the Edit Comments screen.
Now, all we need to do is save our added input values.
2. Save Metabox Values
To save our extra comment values, we want to hook into the comment_save_pre filter. When a comment gets created or updated, the edit_comment function is called. This function performs the proper security checks and calls wp_update_comment which in turn applies the comment_save_pre filter.
add_meta_box('shiba_comment_xtra_box', __('Extra Arguments'), array(&$this,'comment_xtra_metabox'), 'comment', 'normal'); add_filter('comment_save_pre', 'save_mycomment_data' );
Once we hook into wp_update_comment function, we can save our additional comment values by doing the following –
function save_mycomment_data($comment_content) { global $wpdb; $comment_post_ID = absint($_POST['shiba_comment_post_ID']); $comment_parent = absint($_POST['shiba_comment_parent']); $comment_author_IP = esc_attr($_POST['shiba_comment_author_IP']); $comment_ID = absint($_POST['comment_ID']); // comment parent cannot be self if ($comment_parent == $comment_ID) return $comment_content; // don't update // check that post ID exists $post = get_post($comment_post_ID); if (!$post) return $comment_content; // don't update // From wp_update_comment $data = compact('comment_post_ID', 'comment_parent', 'comment_author_IP'); $rval = $wpdb->update( $wpdb->comments, $data, compact( 'comment_ID' ) ); wp_update_comment_count($comment_post_ID); return $comment_content; }
Lines 4-7 – Extract and clean the relevant comment values from our submitted form.
Lines 9-15 – Check that comment parent id and comment post id values are valid. If not, then do not update comment.
Lines 17-19 – Update the comment in our WordPress database.
Line 21 – Update the comment count for the post we are moving our comment to.
And that is all we need to do to expand the Edit Comments screen.