1

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.org/rasmus/ )

2 Answers 2

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.

3
  • .and if I understand correctly, you mean that those codes should be out into the functions.php? But what are you meaning with that the parent theme could interfere? I thought the childtheme was the boss?
    – JoBe
    Commented May 8, 2020 at 14:58
  • @JoBe Yes, pick one of those codes and add it to your functions.php file. If you haven't defined any overwrites (e.g. for comments form) in your child theme, then any modifications in parent theme are used, if none exists, then WP uses its own defaults. Naturally plugin(s) could have some effect, too. Commented May 11, 2020 at 4:44
  • @AntiiKoskinen sorry for the long delay, got prestages of dementia, so I found this questiy again when I was going to write a post about just this issue. Used your comment form default, even thou I don't really understand all the code.. changing code is easy, not creating a new one for me. I have 3 other strings that must be changed, I'll add them to the original question, and would really look forward for your help to solve it, it's my last issue with my site
    – JoBe
    Commented Jun 11, 2020 at 7:03
0

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 );
2
  • @SanjyGupta you mean in the comments.php in the theme/childtheme directory?
    – JoBe
    Commented May 8, 2020 at 14:59
  • Yes. you can make an edit in child theme and parent theme as well. Commented May 11, 2020 at 5:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.