WordPress.org

Make WordPress Core

Opened 5 years ago

Last modified 4 months ago

#31282 new enhancement

finally introduce comments templating

Reported by: ageibert Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.1
Component: Comments Keywords:
Focuses: Cc:
PR Number:

Description

after spending several hours with styling my comments, comment form and using a plugin for the error messages, by default wordpress leads to a new unstyled page showing the errors and only leading back to the homepage if one presses the browser's back button, i'm asking myself why there are no templates and functionality for this all day task.

please introduce:

  • a template for the comments

... yes i know there is comments.php but the comments themself must be templated in a callback function which is terrible (e.g. <?php wp_list_comments( array( 'callback' => 'shape_comment' ) ); ?>)

  • a template for the comment form

... yes i know there is athe possibility to give the comment_form function $args, but this is much more terrible than the comment list "templating", having the need to "template" the comment form in an array (!!!...........)

  • a way that wordpress reloads the current post page if a user sends a comment and is missing required fields like the comment itsself or his/her email address. afterwards showing error messages besides (or wherever my template says so) the comment form fields.

that would be a fundamental improvement and a must.
i can't explain, why everything in wordpress is so easy to template but the comments.

best regards

Change History (3)

#2 @F J Kaiser
5 years ago

[...] but the comments themself must be templated in a callback function

Just a short clarification: The callback will not be used if there is no walker argument set (i.e. a custom Comment Walker class) as the callback is only used inside Walker_Comment::start_el. From 4.1.:

if ( !empty( $args['callback'] ) ) {
        ob_start();
        call_user_func( $args['callback'], $comment, $args, $depth );
        $output .= ob_get_clean();
        return;
}

A custom walker doesn't have to implement it. The same goes for the end-callback argument/callback.

I didn't test this, but I assume that you could load a template part there, using get_template_part(). Example:

// 'callback' => 'getCommentTemplatePart',

function getCommentTemplatePart( $comment, $args, $depth )
{
    // Make `$comment`, `$args` & `$depth` available in the template
    set_query_var( 'comment', $comment );
    set_query_var( 'args', $args );
    set_query_var( 'depth', $depth );

    // load ~/wp-content/your-theme/parts/comment-post.php
    get_template_part( 'parts/comment', 'post' );
}

// Inside parts/comment-post.php then just use `$comment`, `$args` and `$depth`

Above is untested and I'm not sure if it works, but you should give it a try. You can do the same thing for end-callback in case you really need it.

#3 @ageibert
5 years ago

@F J Kaiser
Thanks for that suggestion and it looks really nice! I'll use your code in my next project.

But i think the comments and comment form and error messages/page should be templateable exactly like all other things like pages, posts aso.

The comments should get in the template hierarchy.

Note: See TracTickets for help on using tickets.