How to customize the WordPress comment form

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

WordPress’s default comment box comes with Comment, Name, Email, Website & Cookie consent fields. If we want to replace the website field with a mobile number field we can do that like this:

function bcloud_update_comment_form_field($fields) {
        unset($fields['url']);
        // We need to move cookies field after the phone field
	$cookies_value = $fields['cookies'];
	unset($fields['cookies']);

	$fields[ 'phone' ] = '<p class="comment-form-phone">'.
      '<label for="phone">' . __( 'Mobile Number' ) . '</label>'.
      '<input id="phone" name="phone" type="text" size="30"  tabindex="4" /></p>';

        // if you wanted to replace cookies field text
	$cookies_value = str_replace('website in', 'mobile number in', $cookies_value);
	$fields['cookies'] = $cookies_value;
        return $fields;
}
 
add_filter('comment_form_default_fields', 'bcloud_update_comment_form_field');

Important links – comment_form_default_fields – hook

If we also want to change the before form comment, we can do this like this:

// Add or edit the notes before the comments form
add_filter( 'comment_form_defaults', 'bcloud_update_comment_form_before' );

function bcloud_update_comment_form_before( $defaults ) {
	$defaults['title_reply'] = __( 'Add a Comment' );
	$defaults['comment_notes_before'] = '<p class="comment-notes">Your email address and mobile number will not be published. Required fields are marked <span class="required">*</span></p>';
	return $defaults;
}

Important links – comment_form_defaults – hook, Difference between “comment_form_default_fields” AND “comment_form_fields”

More To Explore

Let's Talk

Ready To Make Your WordPress Project A Success?
Fill out the form to share your contact details.
I'll respond within 24 hours!