Use this if you want to add a checkbox input field to the Comment Reply Form like so, but only on a certain page. In this example, the checkbox will only be added to the comment reply form on the page named ‘report-feedback’. See the page slug on line 3 so you can edit it to your own page slug. HOWEVER, if you want to have the checkbox site-wide on all comment forms, then just comment out line 3 and line 9 below.
In any case, edit the message on line 7 to your liking. Note that in this example, the new checkbox field ID is ‘publicreview’. Don’t change that since that id corresponds to other functions below.
// if is feedback page, add a checkbox field to comments reply form function isa_add_comment_fields($fields) { if( is_page('report-feedback') ) { // comment out this line if you want checkbox on all comment forms $fields['publicreview'] = '<p class="comment-form-public"> <input id="publicreview" name="publicreview" type="checkbox" /> <label for="publicreview"> Check this box to give us permission to publicly post your Review. </label></p>'; } // comment out this line if you want checkbox on all comment forms return $fields; } add_filter('comment_form_default_fields','isa_add_comment_fields');
Save The Checkbox Field and Show It In Admin Backend
While you’re looking at comments in the Admin backend, you may want to see if the checkbox field has been checked off for each comment. The second function below adds this data to the ‘Edit Comments’ page in the WordPress Admin dashboard. If the checkbox has been checked, it will display, “Checkbox is checked: yes“. If the checkbox was not checked, it will display, “Checkbox is checked: no“.
// save the checkbox field function isa_save_comment_meta_checkbox ( $post_id ) { $save_meta_checkbox = $_POST['publicreview']; if ( $save_meta_checkbox == 'on' ) { $value = 'Checkbox is checked: yes'; } else { $value = 'Checkbox is checked: no'; } add_comment_meta( $post_id, 'publicreview', $value, true ); } add_action( 'comment_post', 'isa_save_comment_meta_checkbox', 1 );
Show If Box Was Checked In Admin Backend
This adds a column in the ‘Edit Comments’ page in the WordPress admin backend. This column will show whether your custom checkbox was checked or not.
This goes in functions.php. Change ‘Public’ on the right side of the arrow on line 18 to whatever column heading you want. That is the title that will appear at the top of the checkbox column.
add_action('load-edit-comments.php', 'isa_editcomments_load'); function isa_editcomments_load() { $screen = get_current_screen(); add_filter("manage_{$screen->id}_columns", 'isa_editcomments_add_columns'); } function isa_editcomments_add_columns( $columns ) { $columns = array( 'cb' => '<input type="checkbox" />', 'author' => 'Author', 'comment' => 'Comment', 'isa_publicreview_column_cb' => 'Public',//matches case below 'response' => 'In Response To' ); return $columns; } add_action('manage_comments_custom_column', 'isa_publicreview_column_cb', 10, 2); function isa_publicreview_column_cb($col, $comment_id) { // you could expand the switch to take care of other custom columns switch($col) { case 'isa_publicreview_column_cb': if($t = get_comment_meta($comment_id, 'publicreview', true)) { echo esc_html($t); } else { echo esc_html(''); } break; } }
Shasi
December 22nd, 2012 at 1:21 pm
Where should i use this code? in function.php? comments.php? i need it in every comments.
Isabel
December 23rd, 2012 at 6:04 pm
All of these go in functions.php. To have it on all comments forms, comment out lines 3 and 9 in the first function. Also, I updated this code to add a column in the backend to show if the checkbox was checked. Hope it helps.
Shasi
December 24th, 2012 at 4:26 pm
Thank you very much!
becky
December 1st, 2014 at 6:39 pm
Thanks a lot this was very helpful!
How would I add multiple checkboxes, such as another checkbox saying “I allow my name to be used when my comment is posted”?
Corentin
February 13th, 2016 at 1:56 pm
Thank you !