最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How to tweak comment fields properly from functions.php

programmeradmin1浏览0评论

With the latest version of WordPress, I'm working in my child theme. I have enabled comments on a specific page. I can see OOTB I have author, email, website and comments input areas enabled.

From my functions.php location. I would like to only hide my website field, and change the label of the comments field, from "comment" to "Leave a Review". I have tried the following below, but its hiding everything except the textarea box for "Leave a Review. How can I also bring Author, Email, and Cookie input fields back?

function _ac_comment_fields_custom_order( $fields ) {

    $comment_field = $fields['comment'];
    $author_field = $fields['author'];
    $email_field = $fields['email'];
    $url_field = $fields['url'];
    $cookies_field = $fields['cookies'];

    unset( $fields['comment'] );
    unset( $fields['author'] );
    unset( $fields['email'] );
    unset( $fields['url'] );
    unset( $fields['cookies'] );
   
    $fields['author'] = $author_field;
    $fields['email'] = $email_field;  
    $fields = [ 
        'comment_field' => sprintf(
        '<p class="comment-form-comment">%s %s</p>',
        sprintf(
            '<label for="comment">%s</label>',
            _x( 'Your Review', 'noun' )
        ),
        '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
    )];

    $fields['cookies'] = $cookies_field;
    
    // done ordering, now return the fields:
    return $fields;
}
add_filter( 'comment_form_fields', '_ac_comment_fields_custom_order' );

With the latest version of WordPress, I'm working in my child theme. I have enabled comments on a specific page. I can see OOTB I have author, email, website and comments input areas enabled.

From my functions.php location. I would like to only hide my website field, and change the label of the comments field, from "comment" to "Leave a Review". I have tried the following below, but its hiding everything except the textarea box for "Leave a Review. How can I also bring Author, Email, and Cookie input fields back?

function _ac_comment_fields_custom_order( $fields ) {

    $comment_field = $fields['comment'];
    $author_field = $fields['author'];
    $email_field = $fields['email'];
    $url_field = $fields['url'];
    $cookies_field = $fields['cookies'];

    unset( $fields['comment'] );
    unset( $fields['author'] );
    unset( $fields['email'] );
    unset( $fields['url'] );
    unset( $fields['cookies'] );
   
    $fields['author'] = $author_field;
    $fields['email'] = $email_field;  
    $fields = [ 
        'comment_field' => sprintf(
        '<p class="comment-form-comment">%s %s</p>',
        sprintf(
            '<label for="comment">%s</label>',
            _x( 'Your Review', 'noun' )
        ),
        '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
    )];

    $fields['cookies'] = $cookies_field;
    
    // done ordering, now return the fields:
    return $fields;
}
add_filter( 'comment_form_fields', '_ac_comment_fields_custom_order' );
Share Improve this question asked Jan 8, 2021 at 17:57 klewisklewis 8991 gold badge14 silver badges29 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

You are redeclaring the variable $fields after you define the email key - this basically removes all previous values - instead you need to add a new key for comment_field, for example:

    // retain code before..

    $fields['author'] = $author_field;
    $fields['email'] = $email_field;  
    $fields['comment_field'] = sprintf(
        '<p class="comment-form-comment"><label for="comment">%s</label><textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea></p>',            
        __( 'Your Review', 'theme-textdomain' )
   );

    $fields['cookies'] = $cookies_field;

    // retain code after..

I also reduced your sprintf to one single function, as you only need to use this once, you should also use your own text-domain.

发布评论

评论列表(0)

  1. 暂无评论