Using WordPress ‘get_comments_popup_template()’ PHP function

The get_comments_popup_template() WordPress PHP function retrieves the path of the comment popup template in the current or parent theme.

Usage

$comment_popup_path = get_comments_popup_template();

Parameters

  • None

More information

See WordPress Developer Resources: get_comments_popup_template

Examples

Display the comment popup template path

This example will display the path of the comment popup template.

$comment_popup_path = get_comments_popup_template();
echo 'Comment Popup Template Path: ' . $comment_popup_path;

Customize the comment popup template

This example will customize the comment popup template by adding a custom CSS class to the comment popup link.

$comment_popup_path = get_comments_popup_template();
$comment_popup_link = '<a href="' . $comment_popup_path . '" class="custom-comment-popup-link">View Comments</a>';
echo $comment_popup_link;

Check if comment popup template exists

This example will check if the comment popup template exists in the current or parent theme.

$comment_popup_path = get_comments_popup_template();
if (!empty($comment_popup_path)) {
    echo 'Comment popup template exists.';
} else {
    echo 'Comment popup template does not exist.';
}

Load the comment popup template

This example will load the comment popup template if it exists.

$comment_popup_path = get_comments_popup_template();
if (!empty($comment_popup_path)) {
    require_once($comment_popup_path);
} else {
    echo 'Comment popup template does not exist.';
}

Replace the comment popup template

This example will replace the default comment popup template with a custom one located in the child theme’s folder.

add_filter('comments_popup_template', 'custom_comments_popup_template');

function custom_comments_popup_template($template) {
    $custom_template = get_stylesheet_directory() . '/custom-comments-popup.php';
    if (file_exists($custom_template)) {
        return $custom_template;
    } else {
        return $template;
    }
}