filters - How to add placeholder into comment form textarea?

admin2025-01-08  4

I need to add placeholder="Write a Comment" for default comments form

<div class="comment-field_box">
            <?php
            comment_form(array(
                'title_reply' => '',
                'comment_notes_after' => '',
                'label_submit' => 'Publish',
            ));
            ?>
        </div>

I tried this tip but it didn't work for me.

function custom_comment_form_placeholders( $fields ) {
    if ( isset( $fields['comment'] ) ) {
        $fields['comment'] = str_replace(
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required=""></textarea>',
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="" placeholder="Write a Comment"></textarea>',
            $fields['comment']
        );
    }
    return $fields;
}
add_filter( 'comment_form_defaults', 'custom_comment_form_placeholders' );

I need to add placeholder="Write a Comment" for default comments form

<div class="comment-field_box">
            <?php
            comment_form(array(
                'title_reply' => '',
                'comment_notes_after' => '',
                'label_submit' => 'Publish',
            ));
            ?>
        </div>

I tried this tip but it didn't work for me.

function custom_comment_form_placeholders( $fields ) {
    if ( isset( $fields['comment'] ) ) {
        $fields['comment'] = str_replace(
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required=""></textarea>',
            '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="" placeholder="Write a Comment"></textarea>',
            $fields['comment']
        );
    }
    return $fields;
}
add_filter( 'comment_form_defaults', 'custom_comment_form_placeholders' );

Share Improve this question asked Nov 15, 2024 at 16:14 BlackStar1991BlackStar1991 1334 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Use the filter comment_form_fields instead of comment_form_defaults. Also, in the call to str_replace(), remove the ="" from required in both strings. So, your code will become:

function custom_comment_form_placeholders( $fields ) {
  if ( isset( $fields['comment'] ) ) {
    $fields['comment'] = str_replace(
      '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required></textarea>',
      '<textarea id="comment" name="comment" cols="45" rows="8" maxlength="65525" required placeholder="Write a Comment"></textarea>',
      $fields['comment']
    );
  }
  return $fields;
}
add_filter( 'comment_form_fields', 'custom_comment_form_placeholders' );
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736270198a1410.html

最新回复(0)