Where my_plugin_comment_template is the function WordPress should call when the comment_template() function is called on the theme. Note that the filter function the plugin defines must return the a full path to a template file or the resulting page will be blank.
This is an example of loading a different comments template for a custom post type:
<?php
function my_plugin_comment_template( $comment_template ) {
global $post;
if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
return;
}
if($post->post_type == 'business'){ // assuming there is a post type called business
return dirname(__FILE__) . '/reviews.php';
}
}
add_filter( "comments_template", "my_plugin_comment_template" );
?>
The example code will load the template file reviews.php located in your plugins folder for CPT called business; otherwise, the code uses default template.
Code example migrated from Codex:
A plugin can register as a content filter with the code:
Where
my_plugin_comment_templateis the function WordPress should call when thecomment_template()function is called on the theme. Note that the filter function the plugin defines must return the a full path to a template file or the resulting page will be blank.This is an example of loading a different comments template for a custom post type:
The example code will load the template file
reviews.phplocated in your plugins folder for CPT calledbusiness; otherwise, the code uses default template.This filter is broken/unworking on newer block themes, such as Twenty Twenty-Three.
A replacement to i.e. disable the comments “template” (aka block) would now be: