I have no idea what I’m doing wrong here. My code looks exactly like this guy’s (method 2, toward the bottom), but it’s not working. Here’s my code:
/** Change comment form title **/
function change_comment_fields($fields) {
if (isset($fields)) {
$fields["title_reply"] = "Leave a Comment";
var_dump($fields);
}
return $fields;
}
add_filter( 'comment_form_default_fields', 'change_comment_fields' );
add_filter( 'comment_form_field_comment', 'change_comment_fields' );
Which var_dumps:
array(4) {
["author"]=>
string(207) "<p class="comment-form-author"><label for="author">Name <span class="required">*</span></label> <input id="author" name="author" type="text" value="" size="30" aria-required='true' required='required' /></p>"
["email"]=>
string(235) "<p class="comment-form-email"><label for="email">Email <span class="required">*</span></label> <input id="email" name="email" type="text" value="" size="30" aria-describedby="email-notes" aria-required='true' required='required' /></p>"
["url"]=>
string(125) "<p class="comment-form-url"><label for="url">Website</label> <input id="url" name="url" type="text" value="" size="30" /></p>"
["title_reply"]=>
string(15) "Leave a Comment"
}
So it seems as though the array is being changed properly. “Leave a Reply” doesn’t change, but if I set ‘url’ instead of ‘title_field’, the URL box will change to “Leave a Comment”.
Nevertheless, PHP still spits out this:
Warning: Illegal string offset 'title_reply' in /home/<name>/public_html/<site>/wp-content/themes/baskerville-child/functions.php on line 14
string(113) "Lp class="comment-form-comment">
(Where line 14 corresponds to the where I’m setting $fields[“title_reply”].)
Any ideas? (You can view a sample page here.)
So it seems I didn’t realize what the filter
comment_form_field_comment
does. I was under the misconception that it was to filter the comment form for a visitor who wasn’t signed in. Turns out it’s for filtering the comment textarea text, so yeah, it was passing a string. I thought since the var_dump was printing two copies of the array (I only included one) that both calls were receiving the same data. But upon closer inspection I discovered that the data wasn’t a duplicate (one was fields, the other labels), and that both var_dumps were as a result of applying the first filter.Also, I the first (non-warning-producing) filter was wrong as well. To change the title, I needed to use
comment_form_defaults
.