Comment meta is such a wonderful addition to WordPress that allow us to extend the native comment form with additional fields. This comes very handy for the one's who is trying to create review sites etc. Unfortunately there’s no documentation added on the WP codex.
For the purpose of demonstration we will be doing:
Now that the checkbox isadded, we have to make sure that the data (the checkbox value) is saved to the database. We also want that data to be visible when viewing the comments from within the WordPress admin. So, paste this in your theme’s functions.php file:
The first function will allow comments meta data to be collected and to save it to the database. The second functions will show it in the comments admin pages.
Adding the comments meta data to the notification email proved to be the hardest to figure out – I couldn’t figure out a way to do it via plugin or editing functions.php. So, I decided to do this the least desirable way, which is to edit the WordPress core files, namely wp-includes/pluggable.php.
So find the function wp_notify_postauthor and look for the part that says if (‘comment’ == $comment_type) {. Paste these lines of code in whatever area of the notification email where you would like the comment meta to be included:
Hope this helped, I should give all the credit of this tutorial to this post.
Further Reading:
http://ottopress.com/2010/wordpress-3-0-theme-tip-the-comment-form/
http://www.soapboxdave.com/2010/02/using-wordpress-comment-meta/
For the purpose of demonstration we will be doing:
- add a check box to the comments form.
- show in the WordPress comments admin whether or not the checkbox was selected.
- indicate in the comment notification email whether or not the checkbox was selected.
<label><input id="publishc" name="publishc" tabindex="90" type="checkbox" value="this is the value I want to capture" /> Whatever sentence that you want commenters to use the checkbox for</label>
Now that the checkbox isadded, we have to make sure that the data (the checkbox value) is saved to the database. We also want that data to be visible when viewing the comments from within the WordPress admin. So, paste this in your theme’s functions.php file:
// allow the saving of comment meta data
function fpo_allow_show_comment ( $post_id ) {
$allow_show_comment = $_POST['publishc'];
if ( $allow_show_comment ) {
add_comment_meta( $post_id, 'publishc', $allow_show_comment, true );
}}
add_action( 'comment_post', 'fpo_allow_show_comment', 1 );
// display meta in the edit comments admin page
function show_commeta() {
if (is_admin()) {
echo get_comment_text(), '<br/><br/><strong>', get_comment_meta(get_comment_ID(), 'publishc',1), '</strong>';
}}
add_action('comment_text', 'show_commeta');
The first function will allow comments meta data to be collected and to save it to the database. The second functions will show it in the comments admin pages.
Adding the comments meta data to the notification email proved to be the hardest to figure out – I couldn’t figure out a way to do it via plugin or editing functions.php. So, I decided to do this the least desirable way, which is to edit the WordPress core files, namely wp-includes/pluggable.php.
So find the function wp_notify_postauthor and look for the part that says if (‘comment’ == $comment_type) {. Paste these lines of code in whatever area of the notification email where you would like the comment meta to be included:
$notify_message .= __('Comment meta: ') ;
$notify_message .= sprintf( get_comment_meta($comment->comment_ID, 'publishc',1)) . "\r\n\r\n";
Hope this helped, I should give all the credit of this tutorial to this post.
Further Reading:
http://ottopress.com/2010/wordpress-3-0-theme-tip-the-comment-form/
http://www.soapboxdave.com/2010/02/using-wordpress-comment-meta/
Comments
Post a Comment