adding non-required field to comments
-
After staring at the pages listed below, I have managed to introduce an extra field (not required) to my comment form. I have also managed to get the field to appear in the admin area. If the field is filled in from the front end, it appears in the editing comment area of admin. However, if the field has not been filled in OR if it is emptied in the editing section of admin, the field remains empty and cannot be edited.
This is the coding I have put into my child theme’s functions file:
/*............... add a field to the comment form ...............*/ function add_comment_fields($newfield) { $newfield['webname'] = '<p class="comment-form-author" title="this field is optional"><label for="webname">' . __( 'Website Name' ) . '</label>' . '<input id="webname" name="webname" type="text" size="30" /></p>'; return $newfield; } add_filter('comment_form_default_fields','add_comment_fields'); // add it to the admin area function add_comment_meta_values($comment_id) { if ( ( isset( $_POST['webname'] ) ) && ( $_POST['webname'] != '') ) { $webname = wp_filter_nohtml_kses($_POST['webname']); add_comment_meta($comment_id, 'webname', $webname); } } add_action ('comment_post', 'add_comment_meta_values', 1); // attach it to author function comment_edit_function( $comment_id ) { if( isset( $_POST['webname'] ) ) update_comment_meta( $comment_id, 'webname', esc_attr( $_POST['webname'] ) ); } add_filter( 'get_comment_author_link', 'attach_websitename_to_author' ); function attach_websitename_to_author( $author ) { $webname = get_comment_meta( get_comment_ID(), 'webname', true ); if ( $webname ) $author .= " ($webname)"; return $author; } // Register meta box on comment edit screen function webname_comment_edit_add_meta_box() { add_meta_box( 'title', __( 'website name', 'text-domain' ), 'webname_comment_meta_box', 'comment', 'normal', 'high' ); } add_action( 'add_meta_boxes_comment', 'webname_comment_edit_add_meta_box' ); // Callback function for displaying the comment meta box. function webname_comment_meta_box( $comment ) { $webname = get_comment_meta( $comment->comment_ID, 'webname', true ); wp_nonce_field( 'webname_comment_fields_update', 'webname_comment_fields_update', false ); ?> <p> <label for="webname"><?php _e( 'Website Name', 'text-domain' ); ?></label> <input type="text" name="webname" value="<?php echo esc_attr( $webname ); ?>" class="widefat" /> </p><?php } // Update comment meta data from comment editing screen add_action( 'edit_comment', 'webname_comment_edit_meta_fields' ); function webname_comment_edit_meta_fields( $comment_id ) { if ( ! isset( $_POST['webname_comment_fields_update'] ) || ! wp_verify_nonce( $_POST['webname_comment_fields_update'], 'webname_comment_fields_update' ) ) { return; } if ( ( isset( $_POST['webname'] ) ) && ( $_POST['webname'] != '' ) ) { $webname = wp_filter_nohtml_kses( $_POST['webname'] ); update_comment_meta( $comment_id, 'webname', $webname ); } else { delete_comment_meta( $comment_id, 'webname'); } } $webname = get_comment_meta( $comment->comment_ID, 'webname', true ); echo esc_html( $webname ); /*....... for the admin area from https://wpengineer.com/2214/adding-input-fields-to-the-comment-form/ ....*/ function save_comment_meta_data( $comment_id ) { add_comment_meta( $comment_id, 'webname', $_POST[ 'webname' ] ); } add_action( 'comment_post', 'save_comment_meta_data' );
Where have I gone wrong in the coding, and/or can someone point to where to find the answer? With your answer, please bear in mind that I only know enough about php coding to get myself in trouble….
Thank you for any help you can offer.
(I did try to achieve this using a plugin and ran into exactly the same problem.)
sources:
https://codex.www.ads-software.com/Function_Reference/comment_form
https://wordpress.stackexchange.com/questions/64973/is-it-possible-to-show-custom-comment-metadata-in-the-admin-panel
https://code.tutsplus.com/articles/customizing-comments-in-wordpress-functionality-and-appearance–wp-26128
https://www.wpbeginner.com/plugins/how-to-add-custom-fields-to-comments-form-in-wordpress/
https://saeedpourali.com/custom-field-to-wordpress-comment-form/
https://wpengineer.com/2214/adding-input-fields-to-the-comment-form/
- The topic ‘adding non-required field to comments’ is closed to new replies.