Modify Comment Form Default Fields

Modify Comment Form Default Fields

Modify Comment Form Default Fields

Recently i was hired by a web designer to provide custom coding for their clients comment form.

The client wanted to change the order of the input fields, remove the website URL field and add 2 custom comment form fields named Title and Industry.

Here’s the result i achieved without hacking the WordPress core files and using 3 custom functions to filter the default output via a child theme:

And here’s the code i used to remove the default fields and return the custom fields in the order in the code which also includes 2 small functions to filter 2 of the comment form titles and modify the comment form button text.

add_filter( 'comment_form_defaults', 'wpsites_comment_form_defaults' );
function wpsites_comment_form_defaults( $defaults ) {
 
	$defaults['title_reply'] = __( 'Add Your Story' );
	$defaults['label_submit'] = __( 'Submit Story', 'custom' );
	return $defaults;
}
/**
 * @author    Kishan Kumar - Appfinz Technologies 
 * @example  https://appfinz.com
 * @copyright 2020 Web Designing World
 */

add_filter( 'genesis_title_comments', 'wpsites_title_comments' );
function wpsites_title_comments() {
	$title = '<h3>Your Stories</h3>';
	return $title;
}

add_filter( 'comment_form_default_fields', 'wpsites_comment_form_fields' );

function wpsites_comment_form_fields( $fields ) {

    unset($fields['author']);
    unset($fields['email']);
    unset($fields['url']);

		$fields['title'] = '<p class="comment-form-title">' . '<label for="title">' . __( 'Title' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
		            '<input id="title" name="title" type="text" value="' . esc_attr( $commenter['comment_title'] ) . '" size="30"' . $aria_req . ' /></p>';
					
		$fields['industry'] = '<p class="comment-form-industry"><label for="industry">' . __( 'Industry' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
		            '<input id="industry" name="industry" ' . ( $html5 ? 'type="industry"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_industry'] ) . '" size="30"' . $aria_req . ' /></p>';

		$fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
		            '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' /></p>';
					
		$fields['email']  = '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
		            '<input id="email" name="email" ' . ( $html5 ? 'type="email"' : 'type="text"' ) . ' value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' /></p>';

	return $fields;
}

Once the custom fields have been generated for the front end, there’s more code needed to save the custom data in the database and display it in different admin pages.

You can also display the data in the comment form on the front end.

Modify Comment Form Default Fields

//* Save Custom Comment Form Field Meta Data
add_action( 'comment_post', 'save_custom_comment_field_data', 10, 1 );
function save_custom_comment_field_data( $comment_id ) {
  if ( ( isset( $_POST['industry'] ) ) && ( $_POST['industry'] != '') )
  $industry = wp_filter_nohtml_kses($_POST['industry']);
  add_comment_meta( $comment_id, 'industry', $industry );

  if ( ( isset( $_POST['title'] ) ) && ( $_POST['title'] != '') )
  $title = wp_filter_nohtml_kses($_POST['title']);
  add_comment_meta( $comment_id, 'title', $title );

}
/**
 * @author    Kishan Kumar - Appfinz Technologies 
 * @example  https://appfinz.com
 * @copyright 2020 Web Designing World
 */
//* Add Custom Meta Boxes On Edit Comment Screen
add_action( 'add_meta_boxes_comment', 'wpsites_add_custom_comment_field_meta_boxes' );
function wpsites_add_custom_comment_field_meta_boxes() {
    add_meta_box( 'title', __( 'Title' ), 'wpsites_custom_comment_title_field_meta_box', 'comment', 'normal', 'high' );
	add_meta_box( 'industry', __( 'Industry' ), 'wpsites_custom_comment_industry_field_meta_box', 'comment', 'normal', 'high' );
}

function wpsites_custom_comment_title_field_meta_box( $comment ) {
    $title = get_comment_meta( $comment->comment_ID, 'wpsites_title_comment_field_data', true );
    wp_nonce_field( 'update_comment_title', 'update_comment_title', false );
    ?>
    <p>
        <label for="title"><?php _e( 'Title' ); ?></label>
        <input type="text" name="title" value="<?php echo esc_attr( $title ); ?>" class="widefat" />
    </p>
    <?php
}

function wpsites_custom_comment_industry_field_meta_box( $comment ) {
    $industry = get_comment_meta( $comment->comment_ID, 'wpsites_industry_comment_field_data', true );
    wp_nonce_field( 'update_comment_industry', 'update_comment_industry', false );
    ?>
    <p>
        <label for="industry"><?php _e( 'Industry' ); ?></label>
        <input type="text" name="industry" value="<?php echo esc_attr( $industry ); ?>" class="form-table editcomment" />
    </p>
    <?php
}


add_action( 'edit_comment', 'update_edit_comment' );
function update_edit_comment( $comment_id ) {
    if( ! isset( $_POST['update_comment_title'] ) || ! wp_verify_nonce( $_POST['update_comment_title'], 'update_comment_title' ) ) return;
    if( isset( $_POST['title'] ) )
	    update_comment_meta( $comment_id, 'title', esc_attr( $_POST['title'] ) );
	if( ! isset( $_POST['update_comment_industry'] ) || ! wp_verify_nonce( $_POST['update_comment_industry'], 'update_comment_industry' ) ) return;
    if( isset( $_POST['title'] ) )
		update_comment_meta( $comment_id, 'industry', esc_attr( $_POST['industry'] ) );
}

add_action('load-edit-comments.php', 'add_custom_fields_to_edit_comment_screen');
function add_custom_fields_to_edit_comment_screen() {
    $screen = get_current_screen();

    add_filter("manage_{$screen->id}_columns", 'add_custom_comment_columns');
}

function add_custom_comment_columns($cols) {
    $cols['industry'] = __('Industry', 'wpsites');
	$cols['title'] = __('Title', 'wpsites');
    return $cols;
}

add_action( 'manage_comments_custom_column', 'custom_title_column', 10, 2 );
function custom_title_column($col, $comment_id) {
   
    switch($col) {
        case 'title':
            if($tit = get_comment_meta($comment_id, 'title', true)){
                echo esc_html($tit);
            } else {
                esc_html_e('No Title Submitted', 'wpsites');
            }
    }
}

add_action( 'manage_comments_custom_column', 'custom_industry_column', 10, 2 );
function custom_industry_column($col, $comment_id) {
   
    switch($col) {
        case 'industry':
            if($ind = get_comment_meta($comment_id, 'industry', true)){
                echo esc_html($ind);
            } else {
                esc_html_e('No Industry Submitted', 'wpsites');
            }
    }
}

//* Output Custom Comment Field Data On Comment Form Front End
add_filter( 'comment_text', 'output_title_field_data_comment_form');
function output_title_field_data_comment_form( $text ){


  if( $title = get_comment_meta( get_comment_ID(), 'title', true ) ) {
    $title = '<strong>' . esc_attr( $title ) . '</strong><br/>';
    $text = $title . $text;
    return $text;
	}
  }
 
add_filter( 'comment_text', 'output_industry_field_data_comment_form');
function output_industry_field_data_comment_form( $text ){


  if( $industry = get_comment_meta( get_comment_ID(), 'industry', true ) ) {
    $industry = '<strong>' . esc_attr( $industry ) . '</strong><br/>';
    $text = $industry . $text;
    return $text;
	}
}

Removing Fields

You can use the built in filters to remove fields.

add_filter(‘comment_form_field_url’, ‘__return_false’);

This example removes the website URL field:

add_filter(‘comment_form_field_email’, ‘__return_false’);

User Roles and Permissions in Laravel How to Set Limit Login Attempts in Laravel 7 How To Install CKEditor In Laravel How to Import and Export CSV File in Laravel 7

Appfinz | Website Designing Company | Magento Development Company