WordPress.org

Ready to get started?Download WordPress

Forums

How do I overwrite the comments template? Need help... (14 posts)

  1. Spencer Hill
    Member
    Posted 3 years ago #

    Heya, I'm using the following code to display the comments and comment form:

    <?php comments_template( '', true ); ?>
    <?php comment_form(); ?>

    But I can figure out where I can get the default code to build my template for this form off of. Nor do I know how to create the template itself. Meaning; do I create comment-template.php and WordPress knows how to overwrite that or what?

    I have reviewed the codex but I am still confused. Thanks!

  2. Spencer Hill
    Member
    Posted 3 years ago #

    Bumping this to the top of the forum since I haven't received any replies.

  3. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    But I can figure out where I can get the default code to build my template for this form off of.

    try to use comments.php of twenty ten.
    (if you do, make sure to remove the callback from
    wp_list_comments( array( 'callback' => 'twentyten_comment' ) );)

    for modifying the comment form, see:
    http://codex.wordpress.org/Function_Reference/comment_form

  4. Spencer Hill
    Member
    Posted 3 years ago #

    Thanks for the reply. Comments.php appears to be different than the function that calls whatever file generates comment_form(). Comments.php being just the comments themselves not the file I'm trying to modify...

  5. alchymyth
    The Sweeper & Moderator
    Posted 3 years ago #

    the function 'comment_form()' builds the actual form, see:
    http://codex.wordpress.org/Function_Reference/comment_form

    not very intuitive; however, this seems to be part of more recent themes.

    if you want to have a 'handmade' form, you probably have to go back to older themes, such as the former default theme http://wordpress.org/extend/themes/default, where such a form is part of comments.php

  6. esmi
    Forum Moderator
    Posted 3 years ago #

    You can actually build a pretty decent custom comment form by using the arguments to comment_form and some CSS.

  7. anujasha9
    Member
    Posted 3 years ago #

    The comment form is in the wp-includes/comment-template.php file. If you want to change the form you can change there. (Make sure you know what you are doing.) or else you can replace the <?php comment_form(); ?> and replace it with the this code http://codex.wordpress.org/Styling_Theme_Forms#The_Comments_Form

    then you can do the changes to the code. Hope this is the answer you were looking for

  8. ral_ani
    Member
    Posted 3 years ago #

    I wouldn't recommend what anujasha9 said, it's pretty dangerous to change the source code, this is what I did for mine:

    <?php $comment_args = array( 'fields' => apply_filters( 'comment_form_default_fields', array(
                'author' => '' .
                            '<label for="author">' . __( 'Your Name:' ) . '</label> ' .
                            ( $req ? '<span class="required">*</span>' : '' ) .
                            '<input id="author" name="author" type="text" value="' .
                            esc_attr( $commenter['comment_author'] ) . '" size="30"' . $aria_req . ' />' .
                            '<!-- #form-section-author .form-section -->',
                'email'  => '' .
                            '<label for="email">' . __( 'Your Email:' ) . '</label> ' .
                            ( $req ? '<span class="required">*</span>' : '' ) .
                            '<input id="email" name="email" type="text" value="' . esc_attr(  $commenter['comment_author_email'] ) . '" size="30"' . $aria_req . ' />' .
                    '<!-- #form-section-email .form-section -->',
                'url'    => '' ) ),
                'comment_field' => '' .
                            '<label for="comment">' . __( 'Comment:' ) . '</label>' .
                            '<textarea id="comment" name="comment" cols="45" rows="8" aria-required="true"></textarea>' .
                            '<!-- #form-section-comment .form-section -->',
                'comment_notes_after' => '',
            );
            comment_form($comment_args); ?>

    Or what twentyten does and create your function inside the functions.php file, look up function function twentyten_comment to see what I mean, otherwise here's another good tutorial to read.

  9. esmi
    Forum Moderator
    Posted 3 years ago #

    it's pretty dangerous to change the source code

    Loudly seconded. ral_ani's suggestions are a far better solution.

  10. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    The comment form is in the wp-includes/comment-template.php file. If you want to change the form you can change there.

    This is a terrible suggestion.

    Never, never, never do this!!! Do not modify core WordPress files.

    The comment_form() function is hooked to within inches of its life. It is one of the most customizable functions in all of WordPress. Use the action and filter hooks it provides, if you need to modify it.

  11. Spencer Hill
    Member
    Posted 3 years ago #

    Thank you for the feedback everyone. As a developer I would, obviously, never modify core files unless I was in an emergency (time sensitive situation) and had no alternative. But even then I would seek the proper solution immediately after and undo those changes since modify the core files of well known platform would create frustration for the common developer.

    Chip, I believe I understand filter hooks but I don't have much experience actually utilizing filter hooks (that I'm conscious of). Could you give an example of how I would use one in this case?

    Fundamentally I just want to rearrange how this comment input area is laid out and the labels/text beside each input and inside the button.

    Thank you again everyone!

  12. Spencer Hill
    Member
    Posted 3 years ago #

    ral_ani, thanks for your suggestions and example. That might actually be what I'm looking for. I'm trying to follow WordPress best practices as much as possible so is the consensus that ral_ani's solution would be a fitting solution?

    Thank you!

  13. Chip Bennett
    Theme Review Admin
    Posted 3 years ago #

    Could you give an example of how I would use one in this case?

    Sure!

    Say you wanted to add a custom field, for a CAPTCHA. You would put the markup for it inside of a function, and then hook in that function, e.g.:

    function mytheme_spam_protection() {
       // Some HTML form markup for a CAPTCHA
    }
    add_action( 'comment_form_after_fields' , 'mytheme_spam_protection' );

    ral_ani, thanks for your suggestions and example. That might actually be what I'm looking for.

    Yes, that is a great example. That argument array is fairly powerful all on its own.

  14. luc9999
    Member
    Posted 3 years ago #

    Thank you Rali-ani for the link to the tutorial - i have needded help with that.

    But I have one question - is it better to use the

    <?php comment_form( $args, $post_id ); ?>

    rather that putting the old way which is what that tutorial shows??

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.