comment_form_default_fieldsfilter-hookWP 3.0.0

Allows you to modify, add, or remove comment form fields such as name, email, and website.

These fields are displayed only to logged-out users.

See the step-by-step article How to create new WordPress comment fields to learn how this filter works with other WordPress hooks and functions.

Usage

add_filter( 'comment_form_default_fields', 'wp_kama_comment_form_default_fields_filter' );

/**
 * Function for `comment_form_default_fields` filter-hook.
 * 
 * @param string[] $fields Array of the default comment fields.
 *
 * @return string[]
 */
function wp_kama_comment_form_default_fields_filter( $fields ){

	// filter...
	return $fields;
}
$fields(array)
Array of default comment form fields.

By default, $fields is generated as follows in WordPress core:

$fields   =  array(
		'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" maxlength="245"' . $html_req . ' /></p>',
		'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" maxlength="100" aria-describedby="email-notes"' . $html_req  . ' /></p>',
		'url'    => '<p class="comment-form-url"><label for="url">' . __( 'Website' ) . '</label> ' .
					'<input id="url" name="url" ' . ( $html5 ? 'type="url"' : 'type="text"' ) . ' value="' . esc_attr( $commenter['comment_author_url'] ) . '" size="30" maxlength="200" /></p>',
	);

The callback receives processed strings, and the array looks like this in the English locale:

Array
(
	[author] => <p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" maxlength="245" required='required' /></p>
	[email] => <p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="email" value="" size="30" maxlength="100" aria-describedby="email-notes" required='required' /></p>
	[url] => <p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="url" value="" size="30" maxlength="200" /></p>
)

Examples

#1 Remove the Website field from the comment form

add_filter( 'comment_form_default_fields', 'comment_form_default_add_my_fields' );

/**
 * Removes the Website field from the comment form for logged-out users.
 *
 * @param array $fields Default fields.
 *
 * @return array
 */
function comment_form_default_add_my_fields( $fields ) {
	unset( $fields['url'] );

	return $fields;
}

#2 Add a custom Phone field to the comment form

A custom field can be output through comment_form_default_fields.

The comment_post event must also be used to save the submitted data.

add_filter( 'comment_form_default_fields', 'comment_form_default_add_phone_field' );

/**
 * Adds a Phone field to the comment form for logged-out users.
 *
 * @param array $fields Default fields.
 *
 * @return array
 */
function comment_form_default_add_phone_field( $fields ) {

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

	return $fields;
}

add_action( 'comment_post', 'save_extend_comment_meta_data' );

/**
 * Saves the Phone field contents in comment meta.
 *
 * @param int $comment_id Comment ID.
 */
function save_extend_comment_meta_data( $comment_id ) {
	if ( ! empty( $_POST['phone'] ) ) {
		$phone = sanitize_text_field( $_POST['phone'] );
		add_comment_meta( $comment_id, 'phone', $phone );
	}
}

Use get_comment_meta() to output the field value in a comment template.

Changelog

Since 3.0.0 Introduced.

Where the hook is called

comment_form()
comment_form_default_fields
wp-includes/comment-template.php 2606
$fields = apply_filters( 'comment_form_default_fields', $fields );

Where the hook is used in WordPress

Usage not found.