The comments_template WordPress PHP filter allows you to modify the path to the theme template file used for the comments template.
Usage
add_filter('comments_template', 'your_custom_function_name'); function your_custom_function_name($theme_template) { // your custom code here return $theme_template; }
Parameters
$theme_template
(string) – The path to the theme template file.
More information
See WordPress Developer Resources: comments_template
Examples
Change comments template to a custom one
Modify the path to use a custom comments template in your theme.
add_filter('comments_template', 'use_custom_comments_template'); function use_custom_comments_template($theme_template) { $custom_template = get_stylesheet_directory() . '/custom-comments.php'; return $custom_template; }
Use different comments templates for different post types
Use different comments templates for posts and pages.
add_filter('comments_template', 'different_comments_template_for_post_types'); function different_comments_template_for_post_types($theme_template) { if (is_page()) { $theme_template = get_stylesheet_directory() . '/page-comments.php'; } else { $theme_template = get_stylesheet_directory() . '/post-comments.php'; } return $theme_template; }
Disable comments for specific categories
Disable comments for posts in a specific category by using an empty comments template.
add_filter('comments_template', 'disable_comments_for_category'); function disable_comments_for_category($theme_template) { $disable_comments_category = 'no-comments'; if (in_category($disable_comments_category)) { $theme_template = get_stylesheet_directory() . '/empty-comments.php'; } return $theme_template; }
Changing the Comments Template Path
add_filter('comments_template', function($theme_template) { // change the path to the comments template file $theme_template = get_stylesheet_directory() . '/custom-comments-template.php'; return $theme_template; });
In the above example, we’re using the get_stylesheet_directory()
function to get the path to our theme’s directory, and appending the path to our custom comments template file. This will ensure that WordPress uses our custom template file for the comments section.
Removing Comments Section from a Page
add_filter('comments_template', function($theme_template) { // check if the current page is the "Contact" page if(is_page('contact')) { // return an empty string to remove the comments section return ''; } return $theme_template; });
In the above example, we’re checking if the current page is the “Contact” page using the is_page()
function. If it is, we’re returning an empty string, which will remove the comments section from the page.
Adding Custom HTML to the Comments Section
add_filter('comments_template', function($theme_template) { // your custom code here to add HTML to the comments section $custom_html = '<p>This is my custom HTML for the comments section!</p>'; $theme_template = $theme_template . $custom_html; return $theme_template; });
In the above example, we’re using the $theme_template
parameter to add custom HTML to the comments section. We’re adding the HTML to a variable called $custom_html
, and then appending it to the $theme_template
parameter.
Redirecting Users After Comment Submission
add_filter('comment_post_redirect', function($location) { // your custom code here to redirect users after comment submission return 'http://example.com/thank-you-for-your-comment/'; });
In the above example, we’re using the comment_post_redirect
filter to redirect users to a custom URL after they submit a comment. We’re using the $location
parameter to set the URL, and then returning the modified $location
.
Customizing the Comments Form
add_filter('comment_form_defaults', function($defaults) { // your custom code here to customize the comments form $defaults['comment_notes_before'] = '<p>Your custom text here</p>'; return $defaults; });
In the above example, we’re using the comment_form_defaults
filter to customize the comments form. We’re modifying the $defaults
parameter to add custom text before the comments form, and then returning the modified $defaults
.