I want to create custom design for the comments. comments_template() will load the default \comments.php.

i have a file inside my plugin commentsnew.php how can i use this file for comments_template()?

file is getting loaded relative to theme directory.

apply_filters( 'comments_template', string $theme_template ) this completely overrides the template but don't want a complete override but apply only if used.

  • Can you provide some context for why you would want to do this or the problem that this solves? – Tom J Nowell Aug 29 '19 at 18:24
  • @TomJNowell to have a review template for the custom post that is created inside a plugin – Latheesh V M Villa Aug 29 '19 at 18:38

You can use the comments_template filter to alter which file your CPT will use for comments.

function wpse_plugin_comment_template( $comment_template ) {
     global $post;
     if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
        // leave the standard comments template for standard post types
     if($post->post_type == 'business'){ // assuming there is a post type called business
        // This is where you would use your commentsnew.php, or review.php
        return dirname(__FILE__) . '/review.php';
// throw this into your plugin or your functions.php file to define the custom comments template.
add_filter( "comments_template", "wpse_plugin_comment_template" );
  • Thank you . but this completely overrides the template for the post type` business` but i needed a case where business can have multiple comments templates and and use them as needed – Latheesh V M Villa Aug 29 '19 at 19:16
  • 1
    You should be able to use this same model, changing the "if" statement to whatever criteria you require for your workflow. For that matter, you could have multiple comments templates, and swap among them using a switch ... case statement to select which to use. In any case, add_filter() would get the process started for you. – Mike Baxter Aug 29 '19 at 19:20
  • got it thank you :) – Latheesh V M Villa Aug 29 '19 at 19:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.