• Resolved Nox

    (@profnox)


    Hi,

    I need to change the comment form template in order to remove labels and add some placeholder in the different fields.
    But the template seems to be “hard-coded”. I discovered that the template is managed by the function comment_form() in wp-includes/comment-template.php.

    Is it a way to override this function ?
    Or a proper way to edit the comment form template ?

    Thank you in advance,

    N.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Thread Starter Nox

    (@profnox)

    I don’t really get why my question was moved to the “Fixing” section…
    Aren’t native templates modification in a from-scratch custom theme considered as development ?

    • This reply was modified 7 years, 6 months ago by Nox.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    Howdy, howdy!

    Yes, it is very possible to change that. At least the fields. That’s done using a filter. That code can be found: https://developer.wordpress.org/reference/functions/comment_form/#source

    It’s this line: $fields = apply_filters( 'comment_form_default_fields', $fields );

    So what you could potentially do is something like:

    
    add_filter( 'comment_form_default_fields', function( $fields ) {
      // do things here
      return $fields;
    }, 10 );
    

    Do note that code will only work for PHP version 5.3 and up.

    Unless you mean something different.

    Thread Starter Nox

    (@profnox)

    Oh, thanks you ! It works well.

    I’m still taking my first step in theme development, so I didn’t really understand how the to use the filters and how to find them.

    So I use the same process to hook and custom the “comment” field (here the fields array were just used to manage extra fields like authors, url).

    I share the result, here’s an example where I custom all fields of the comment form (adding placeholder to the fields, and removing the “website” field by the way) :

    <?php
    
    // Edit "extra"-fields (author, email, url...) of comment form
    
    add_filter( 'comment_form_default_fields', 'custom_comment_form_fields', 10 );
    
    function custom_comment_form_fields( $fields ) {
    
        $commenter = wp_get_current_commenter();
        $req      = get_option( 'require_name_email' );
        $html_req = ( $req ? " required='required'" : '' );
    
        $fields['author'] = '<p class="comment-form-author">' . '<label for="author">' . __( 'Name' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
                '<input placeholder="' . __( 'Name' ) . '" id="author" name="author" type="text" value="' . esc_attr( $commenter['comment_author'] ) . '" size="30" maxlength="245" ' . $html_req . ' /></p>';
        $fields['email'] = '<p class="comment-form-email"><label for="email">' . __( 'Email' ) . ( $req ? ' <span class="required">*</span>' : '' ) . '</label> ' .
                '<input placeholder="' . __( 'Email' ) .'" id="email" name="email" ' . 'type="email"' . ' value="' . esc_attr( $commenter['comment_author_email'] ) . '" size="30" maxlength="100" aria-describedby="email-notes" ' . $html_req . ' /></p>';
        unset($fields['url']);
    
        return $fields;
    }
    
    // Edit the "comment" text-area
    
    add_filter('comment_form_defaults', 'custom_comment_field', 10);
    
    function custom_comment_field ($defaults) {
        $defaults['comment_field'] = '<p class="comment-form-comment"><label for="comment">' . _x( 'Comment', 'noun' ) . '</label> <textarea placeholder="' . _x( 'Comment', 'noun' ) . '" id="comment" name="comment" cols="45" rows="8" maxlength="65525" required="required"></textarea></p>';
        return $defaults;
    }
    

    N.

    • This reply was modified 7 years, 6 months ago by Nox.
    Moderator Jose Castaneda

    (@jcastaneda)

    THEME COFFEE MONKEY

    I’m still taking my first step in theme development

    That’s awesome! You’ll have so many aggravating moments but it’s so fun too!

    There are several articles which will help you understand hooks and filters a lot better. The way I often remember it is via photography. A filter is much like a neutral density filter added to the lens and a hook is like the self-timer; the time between the shutter click and shutter release is about when the hook will act or do things.

Viewing 4 replies - 1 through 4 (of 4 total)

The topic ‘Override the comment_form function ?’ is closed to new replies.