钩子文档

comment_form_fields

💡 云策文档标注

概述

comment_form_fields 是一个 WordPress 过滤器,用于修改评论表单的字段,包括文本区域。它允许开发者自定义字段的顺序、内容或移除特定字段。

关键要点

  • 过滤器名称:comment_form_fields,参数为 $comment_fields 数组,包含评论表单的字段。
  • 默认字段包括 author、email、url 和 comment,可通过过滤器进行添加、删除或重排。
  • 常用于调整字段显示顺序(如将评论框移至底部)或自定义字段内容。

代码示例

// 示例:将评论字段移至底部
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 );

注意事项

  • 使用过滤器时需确保保留必要的 HTML 结构(如默认的 ID、类、for 和 name 属性),以维持表单功能。
  • 可结合 CSS 需求调整标签和输入元素的位置,例如使用 div 替代 p 元素。

📄 原文内容

Filters the comment form fields, including the textarea.

Parameters

$comment_fieldsarray
The comment fields.

Source

$comment_fields = apply_filters( 'comment_form_fields', $comment_fields );

Changelog

Version Description
4.4.0 Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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 5 content

    Move the comment text field to the bottom.

    /**
     * Move the comment text field to the bottom.
     *
     * @see 	<a href="https://developer.wordpress.org/reference/hooks/comment_form_fields/" rel="ugc">https://developer.wordpress.org/reference/hooks/comment_form_fields/</a>
     * @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 6 content

    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).