Using WordPress ‘comments_popup_script()’ PHP function

The comments_popup_script() WordPress PHP function displays the JavaScript popup script that enables the display of a comment in a new popup window.

Usage

Here’s a basic usage of the comments_popup_script() function:

comments_popup_script();

As this function doesn’t take any parameters, it’s as simple as calling it to display the JavaScript popup script for comments.

Parameters

The comments_popup_script() function doesn’t require any parameters.

More information

See WordPress Developer Resources: comments_popup_script()

This function was introduced in WordPress version 0.71. As of the last update, it is not deprecated and is still in use.

Examples

Basic usage of comments_popup_script()

This example just calls the comments_popup_script() function. This will output the JavaScript necessary for a comment popup window.

comments_popup_script();

Using comments_popup_script() in a post loop

In this example, the comments_popup_script() function is used inside a post loop. This will display the comment popup script for each post in the loop.

if ( have_posts() ) {
    while ( have_posts() ) {
        the_post();
        comments_popup_script();
    }
}

Conditional usage of comments_popup_script()

This example uses the comments_popup_script() function only when comments are open for the post.

if ( comments_open() ) {
    comments_popup_script();
}

Using comments_popup_script() with a custom callback

In this example, the comments_popup_script() function is used with a custom callback for when the comment popup script is displayed.

function custom_callback() {
    echo 'Comment popup script displayed!';
}
add_action( 'comment_popup_script', 'custom_callback' );

Removing comments_popup_script() action

This example removes the comments_popup_script() function from the ‘wp_head’ action, preventing the comment popup script from being displayed.

remove_action( 'wp_head', 'comments_popup_script' );