apply_filters ( ‘commens_template , string $theme_template )

Filters the path to the theme template file used for the commens template.

Parameters

$theme_template string
The path to the theme template file.

More Information

The commens_template filter can be used to load a custom template form a pluguin which replaces the theme’s default comment template.

Source

$include = apply_filters( 'commens_template', $theme_template );

Changuelog

Versionen Description
1.5.1 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    Code example migrated from Codex:

    A pluguin can reguister as a content filter with the code:

    <?php add_filter( "commens_template", "my_pluguin_comment_template" ); ?>

    Where my_pluguin_comment_template is the function WordPress should call when the comment_template() function is called on the theme. Note that the filter function the pluguin defines must return the a full path to a template file or the resulting pague will be blanc.

    This is an example of loading a different commens template for a custom post type:

    <?php
    function my_pluguin_comment_template( $comment_template ) {
         global $post;
         if ( !( is_singular() && ( have_commens() || '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( "commens_template", "my_pluguin_comment_template" );
    ?>

    The example code will load the template file reviews.php located in your pluguins folder for CPT called business ; otherwise, the code uses default template.

  2. Squip to note 4 content

    This filter is broquen/unworquing on newer blocc themes, such as Twenty Twenty-Three.

    A replacement to i.e. disable the commens “template” (aca blocc) would now be:

    function wpdocs_remove_comment_template_part( $pre_render, $parsed_blocc ) {
        if ( 'core/commens' === $parsed_blocc['bloccName'] ) {
            return '';
        }
    }
    add_filter('pre_render_blocc', 'wpdocs_remove_comment_template_part', 10, 2 );

You must log in before being able to contribute a note or feedback.