apply_filters( 'comment_form_fields', array $comment_fields )

Filters the comment form fields, including the textarea.


Parameters Parameters

$comment_fields

(array) The comment fields.


Top ↑

Source Source

File: wp-includes/comment-template.php

View on Trac



Top ↑

Changelog Changelog

Changelog
Version Description
4.4.0 Introduced.

Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note 1 content
    Contributed by Andrija Naglic

    These are the default comment form fields, as seen here https://developer.wordpress.org/reference/functions/comment_form/

        $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"' . $aria_req . $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"' . $aria_req . $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>',
        );
  2. Skip to note 2 content
    Contributed by Mahdi Yazdani

    Move the comment text field to the bottom.

    /**
     * Move the comment text field to the bottom.
     *
     * @see 	https://developer.wordpress.org/reference/hooks/comment_form_fields/
     * @param  	array  $fields 		The comment fields..
     * @return 	array
     */
    function prefix_move_comment_field_to_bottom( $fields ) {
    
    	$comment_field = $fields['comment'];
    	unset( $fields['comment'] );
    	$fields['comment'] = $comment_field;
    	return $fields;
    
    }
    add_filter( 'comment_form_fields',      'prefix_move_comment_field_to_bottom', 10, 1 );
    
  3. Skip to note 3 content
    Contributed by icedterminal

    To override the comment form fields to supply your own with modifications, without a plugin, here is an example that would go in your functions.php file:

    add_filter( 'comment_form_fields', 'custom_comment_field' );
    function custom_comment_field( $fields ) {
        // What fields you want to control.
        $comment_field = $fields['author'];
        $comment_field = $fields['email'];
        $comment_field = $fields['comment'];
        $comment_field = $fields['cookies'];
    
        // The fields you want to unset (remove).
        unset($fields['author']);
        unset($fields['email']);
        unset($fields['url']);
        unset($fields['comment']);
        unset($fields['cookies']);
    
        // Display the fields to your own taste.
        // The order in which you place them will determine in what order they are displayed.
        $fields['author'] = '<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label><input type="text" id="author" name="author" require="required" placeholder="Name"></p>';
        $fields['email'] = '<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label><input type="text" id="email" name="email" require="required" placeholder="Email"></p>';
        $fields['comment'] = '<p class="comment-form-comment"><label for="comment">Comment <span class="required">*</span></label><textarea id="comment" name="comment" required="required" placeholder="Comment"></textarea></p>';
        $fields['cookies'] = '<p class="comment-form-cookies-consent"><input id="wp-comment-cookies-consent" name="wp-comment-cookies-consent" type="checkbox" value="yes"><label for="wp-comment-cookies-consent">Save details for future comments?</label></p>';
        return $fields;
    }
    

    This will render the complete form for users not logged in, and render only the comment field for those logged in. For the form to function properly, default IDs, Classes, For, and Names should be included. You may add additional classes, replace the p element with a div, and/or move the label to be after the input or textarea for CSS reasons (e.g. Material design).

You must log in before being able to contribute a note or feedback.