Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

Editing comment form default fields

  • Hey there!

    I have a similar problem like this user.

    I want to customize my comment form. First I want to change the comment form title and second I’d like to add label elements to the input fields author and email – I used two filters to achieve this.

    add_filter( 'generate_leave_comment','ryb_custom_leave_comment' );
    function ryb_custom_leave_comment() {
        return 'Ich freue mich über deinen Kommentar:';
    }

    and

    add_filter( 'comment_form_default_fields', 'ryb_change_comment_form_default_fields' );
    function ryb_change_comment_form_default_fields( $fields ) {
        $commenter = wp_get_current_commenter();
        $required = get_option( 'require_name_email' );
    
        $fields['author'] = sprintf(
            '<label for="author">%1$s</label><input placeholder="%1$s%3$s" id="author" name="author" type="text" value="%2$s" size="30"%4$s />',
            esc_html__( 'Name', 'generatepress' ),
            esc_attr( $commenter['comment_author'] ),
            $required ? ' *' : '',
            $required ? ' required' : ''
        );
    
        $fields['email'] = sprintf(
            '<label for="email">%1$s</label><input placeholder="%1$s%3$s" id="email" name="email" type="email" value="%2$s" size="30"%4$s />',
            esc_html__( 'Email', 'generatepress' ),
            esc_attr( $commenter['comment_author_email'] ),
            $required ? ' *' : '',
            $required ? ' required' : ''
        );
    
        return $fields;
    }

    The first one works and the title has changed as you can see while looking at the site. But the second filter won’t apply regardless of what I’m doing. I already cleared plugin and server cache and even disabled them completely but the changes won’t apply. I just removed the is-screen-reader-class (or something like that) from the label elements so that I can use them as regular labels.

    What am I doing wrong? I add PHP via functions.php of my child theme.

    Cheers
    Philipp

  • Hi there,

    you could try making the filter callback fire later.

    For examples, this line in your code:

    add_filter( 'comment_form_default_fields', 'ryb_change_comment_form_default_fields' );

    Change it to:

    add_filter( 'comment_form_default_fields', 'ryb_change_comment_form_default_fields' ,99 );

    If that don’t work, try:

    
    add_action('wp', function(){
        add_filter( 'comment_form_default_fields', 'ryb_change_comment_form_default_fields' );
    });
    
  • Hey,

    ah.. I forgot priority is a thing 🤦‍♂️ That works like a charm, thanks David.

    Cheers
    Philipp

  • Awesome – glad to hear that worked !

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.