Site logo

Archived topic

Adding some text to the comment form

7 replies · Started by Bandara on September 11, 2021

Viewing posts 1–8 of 8

I am trying to add some additional instructions to the comment form. I have tried the suggestion here, but it doesn't seem to do anything.

However, it does throw this warning:
Warning: call_user_func_array() expects parameter 1 to be a valid callback, function 'tu_change_comment_fields' not found or invalid function name in /home/dh_test_sc_9is7g3/test.serenecolombo.org/wp-includes/class-wp-hook.php on line 303

Do you have a suggestion?

Hi there,

Can you let me know exactly where in the comment form you are trying to add this?

Thanks.

Sure. I'd like to put some text below "Leave a Comment".

I'd also like to change the instructional text in the name field and email field. Currently it says "Name *" and "Email *" I'd like to change it to "First Name *" and "Email (won't be published) *"

Hi Bandara,

There's no hook directly after "Leave a Comment" but you can filter the text itself to add something below it.

Example:

add_filter( 'generate_leave_comment','tu_custom_leave_comment' );
function tu_custom_leave_comment() {
    $default_string = 'Leave a Comment'
    $added_text = 'add your text here'
    $new_output = $default_string. '<br>' .$added_text;
    return '$new_output';
}

As for changing the labels of the form fields, you can either use the generate_filter_comment_fields filter
https://github.com/tomusborne/generatepress/blob/b60b853630da6d9015722da903e53c8064148b0a/inc/structure/comments.php#L142-L176

Example.

add_filter( 'comment_form_default_fields', function($fields){

	$commenter = wp_get_current_commenter();
	$required = get_option( 'require_name_email' );

	$fields['author'] = sprintf(
		'<label for="author" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="author" name="author" type="text" value="%2$s" size="30" />',
		esc_html__( 'First Name', 'generatepress' ),
		esc_attr( $commenter['comment_author'] ),
		$required ? ' *' : ''
	);

	$fields['email'] = sprintf(
		'<label for="email" class="screen-reader-text">%1$s</label><input placeholder="%1$s%3$s" id="email" name="email" type="email" value="%2$s" size="30" />',
		esc_html__( 'Email (won’t be published)', 'generatepress' ),
		esc_attr( $commenter['comment_author_email'] ),
		$required ? ' *' : ''
	);

	$fields['url'] = sprintf(
		'<label for="url" class="screen-reader-text">%1$s</label><input placeholder="%1$s" id="url" name="url" type="url" value="%2$s" size="30" />',
		esc_html__( 'Website', 'generatepress' ),
		esc_attr( $commenter['comment_author_url'] )
	);

	return $fields;

}, 20, 1 );

Or use its text domain and filter it using gettext.

Example:

add_filter( 'gettext', function( $text ) {
    if ( 'Name' === $text ) {
        return 'First Name';
    } if ( 'Email' === $text ) {
        return 'Email (won’t be published)';
    }

    return $text;
} );

Thanks!

So the first bit of code that starts with add_filter( 'generate_leave_comment','tu_custom_leave_comment' ); did add the text, however all of it ends up inside h3 tags. I was hoping to just have regular text below the heading.

With some more searching and experimenting, I found this and it seems to do what I am looking for:

add_filter('comment_form_defaults', 'change_note_after_comment_form',99);
function change_note_after_comment_form($defaults) {
	$defaults['comment_notes_before'] = '<p class="comment-notes"> text 2 </p>';
	return $defaults;
}

The other code worked perfectly. Thank you!

I did have a question about the last one, if that's ok...

add_filter( 'gettext', function( $text ) {
    if ( 'Name' === $text ) {
        return 'First Name';
    } if ( 'Email' === $text ) {
        return 'Email (won’t be published)';
    }

    return $text;
} );

Would this replace the text "Name" etc everywhere on the site? How does the code know to only do it on the comments form?

I've done a recheck of the actual theme code files and I think i missed generate_below_comments_title.

You can remove the filter I've provided for it and just use this hook to place a text below the comments title.

You can use Hook Element or Block element to add. generate_below_comments_title should be available on the hook options. :D

Would this replace the text “Name” etc everywhere on the site? How does the code know to only do it on the comments form?

Yeah that's something to consider but this will only apply to 'Name' strings under generatepress textdomain so it won't apply to all Name string.

I think the best approach would be the generate_filter_comment_fields which I also provided on the previous reply.
It's best as it's real specific. :D

Thank you! So happy I am using GP Pro!

No problem. glad to be of any help. :D

This archived topic is closed to new replies.