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

functions - How do I change parameters without changing the core

programmeradmin0浏览0评论

How do I change a parameter/text that are specified in the core without editing the core?

For example, this is a part of of comment-template.php from the wp-includes directory:

$fields = apply_filters( 'comment_form_default_fields', $fields );

    $defaults = array(
        'fields'               => $fields,
        'comment_field'        => sprintf(
            '<p class="comment-form-comment">%s %s</p>',
            sprintf(
                '<label for="comment">%s</label>',
                _x( 'Comment', 'noun' )
            ),
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
        ),

That's a part of the thingie that displays the comment form, and writes the word "Comment" above the text field.

Now, if I want to change the word Comment to Please leave a comment..., How do I do it?

I understand that some kind of hook need to be used and placed in the functions.php file, but there ends my knowledge, and just change the core file is a big NO-NO!

Edit:

On line 2433 (wp 5.4.2)

'title_reply'          => __( 'Leave a Reply' ),

I just changed it to;

        'title_reply

'          => __( '' ),

I just don't want to show it..

Line 2441

'label_submit'         => __( 'Post Comment' ),

changed it to:

'label_submit'         => __( ' Submit your thoughts...' ),

And finally (almost), line 2531

echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );

Changed it to

echo apply_filters( 'comment_form_logged_in', '' );

Don't want to display this either..

And on line 2420

sprintf(
                '<span id="email-notes">%s</span>',
                __( 'Your email address will not be published.' )
            ),

That one I don't want to be shown at all..

That's how I previously has changed the core comment-temple.php file.

How do I do it in the functions.php

(It will affect pages like: / )

How do I change a parameter/text that are specified in the core without editing the core?

For example, this is a part of of comment-template.php from the wp-includes directory:

$fields = apply_filters( 'comment_form_default_fields', $fields );

    $defaults = array(
        'fields'               => $fields,
        'comment_field'        => sprintf(
            '<p class="comment-form-comment">%s %s</p>',
            sprintf(
                '<label for="comment">%s</label>',
                _x( 'Comment', 'noun' )
            ),
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
        ),

That's a part of the thingie that displays the comment form, and writes the word "Comment" above the text field.

Now, if I want to change the word Comment to Please leave a comment..., How do I do it?

I understand that some kind of hook need to be used and placed in the functions.php file, but there ends my knowledge, and just change the core file is a big NO-NO!

Edit:

On line 2433 (wp 5.4.2)

'title_reply'          => __( 'Leave a Reply' ),

I just changed it to;

        'title_reply

'          => __( '' ),

I just don't want to show it..

Line 2441

'label_submit'         => __( 'Post Comment' ),

changed it to:

'label_submit'         => __( ' Submit your thoughts...' ),

And finally (almost), line 2531

echo apply_filters( 'comment_form_logged_in', $args['logged_in_as'], $commenter, $user_identity );

Changed it to

echo apply_filters( 'comment_form_logged_in', '' );

Don't want to display this either..

And on line 2420

sprintf(
                '<span id="email-notes">%s</span>',
                __( 'Your email address will not be published.' )
            ),

That one I don't want to be shown at all..

That's how I previously has changed the core comment-temple.php file.

How do I do it in the functions.php

(It will affect pages like: https://rainbowpets/rasmus/ )

Share Improve this question edited Jun 14, 2020 at 14:28 JoBe asked May 7, 2020 at 7:11 JoBeJoBe 1712 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

If you scroll further down the comment-template.php file, you'll notice that the next available filter you can use is comment_form_defaults. With this filter you can change the default comment form configuration.

add_filter( 'comment_form_defaults', 'filter_comment_form_defaults' );
function filter_comment_form_defaults( $defaults ) {

  $defaults['comment_field'] = sprintf(
        '<p class="comment-form-comment">%s %s</p>',
        sprintf(
            '<label for="comment">%s</label>',
            _x( 'Please leave a comment...', 'Comment field label', 'textdomain' )
        ),
        '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
    );

  return $defaults;
} 

But your (parent) theme might also make some modifications to the fields so the default values might get overwritten. So just keep scrolling down and you'll eventually see comment_form_fields filter. This filters the comment form fields, including the textarea.

add_filter( 'comment_form_fields', 'filter_comment_form_fields' );
function filter_comment_form_fields( $fields ) {

  $fields['comment'] = sprintf(
        '<p class="comment-form-comment">%s %s</p>',
        sprintf(
            '<label for="comment">%s</label>',
            _x( 'Please leave a comment...', 'Comment field label', 'textdomain' )
        ),
        '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
    );

  return $fields;
} 

If you only want to target the comment field, then below the previous filter, you can see in a foreach loop the comment_form_field_comment filter.

add_filter( 'comment_form_field_comment', 'filter_comment_form_field_comment' );
function filter_comment_form_field_comment( $field ) {

  return sprintf(
        '<p class="comment-form-comment">%s %s</p>',
        sprintf(
            '<label for="comment">%s</label>',
            _x( 'Please leave a comment...', 'Comment field label', 'textdomain' )
        ),
        '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea>'
    );

} 

Please refer to the WP code reference (links) for more details about the filters.

Yes, You can customize the comment form without affecting the core file.

You just need to replace the code of comment form with the below code inside the comments.php in the activated theme.

$args = array(
    'comment_field' => '<p class="comment-form-comment"><label for="comment">' . _x( 'Please leave a comment...', 'noun' ) . '</label><textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea></p>'
  'fields' => apply_filters( 'comment_form_default_fields', array(

    'author' =>
      '<p class="comment-form-author">' .
      '<label for="author">' . __( 'Name', 'domainreference' ) . '</label> ' .
      ( $req ? '<span class="required">*</span>' : '' ) .
      '<input id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) .
      '" size="30"' . $aria_req . ' /></p>',

    'email' =>
      '<p class="comment-form-email"><label for="email">' . __( 'Email', 'domainreference' ) . '</label> ' .
      ( $req ? '<span class="required">*</span>' : '' ) .
      '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) .
      '" size="30"' . $aria_req . ' /></p>',

    'url' =>
      '<p class="comment-form-url"><label for="url">' .
      __( 'Website', 'domainreference' ) . '</label>' .
      '<input id="url" name="url" type="text" value="' . esc_attr( $commenter['comment_author_url'] ) .
      '" size="30" /></p>'
    )

);
comment_form( $args );
发布评论

评论列表(0)

  1. 暂无评论