adding and editing non-required field in comments
-
In spite of only really knowing enough about php coding to get myself in trouble, I find I cannot stop trying anyway….
So far, 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 extra 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. (I did try to achieve this using a plugin and ran into exactly the same problem.)
Staring at various parts of the Codex as well as googling, I am wondering if I need to introduce
update_post_meta($meta_value);
AND/ORupdate_user_meta( $prev_value )
For either of these, I am entirely unclear on how to add them to my child theme’s functions file.
Where have I gone wrong in the following, and/or can someone point to where to find the answer? This is the coding I have put into my child theme’s functions file so far:
/*............... 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' );
Please note that this is an update on the question I posed in “fixing wordpress” that, on reflection (there have been zero responses), possibly does not belong there.
Thank you for any help you can offer.
- The topic ‘adding and editing non-required field in comments’ is closed to new replies.