How to override the comments form in a child theme

This is a quick post on how to provide your own comments template in your child theme ( overriding comments.php in your parent theme). You can just as easily use the same code to override comments.php if you aren’t using a child theme.

Todo this you need to you need to use the filter ‘comments_template’ ( see the wordpress codex on this filter here)

function greenbox_override_comment_template( $comment_template ) {
    
     global $post;
     if ( !( is_singular() && ( have_comments() || 'open' == $post->comment_status ) ) ) {
        return;
     }
     
     
        
        // your full path and name of comments file to use
        return dirname(__FILE__) ."/my-comments.php";
     
}

add_filter( "comments_template", "greenbox_override_comment_template" );

 

A handy thing you might sometimes want todo is to have a custom comments template, for various custom post types. If you did want todo this you could just add in an if to encase the returned path and filename. e.g.

 

if($post->post_type == 'my_custom_post_type'){

         return dirname(__FILE__) ."/my_custom_post_type-comments.php";

}

 

Hope someone finds these snippets for how to override comments.php useful.

 

 

1 thought on “How to override the comments form in a child theme”

Leave a Comment