1

I want to show comments in a popup for the page with id=22.

This is the anchor on whose click the popup will open:

<a class="button button-small button-line-light comment-popup" 
    href="<?php echo get_stylesheet_directory_uri();?>/comments-detail.php">View All</a>

If I have plain html in comments-detail.php, popup shows the html.

But when I try to get the comments list of page id=22, in comments-detail.php, with the following simple code:

<?php 
    $args = array(
        'post_id' => 22
    );
    $comments = get_comments( $args );
?>

then I get the error:

call to undefined function get_comments()

What am I doing wrong or is my approach totally wrong?

1 Answer 1

2

You're re-inventing the wheel here ;-)

The Problem

You're bypassing WordPress with this approach, so that's explains the error message.

Accessing any custom file, within your theme directory, will not load WordPress core automatically.

Already available

This is already supported by the WordPress core.

You should check out comments_popup_link() and comments_popup_script().

In general, you can get all your comments, for a given post with:

example.tld/?comments_popup=123

where 123 is some post ID.

You can override the default comment popup template, by placing your own version of the comments-popup.php file, in the current theme directory.

The default one is located in /wp-includes/theme-compat/comments-popup.php.

Note on WP 4.2.2 - Incorrect inline documentation

In core we got this description:

  • The template path is filterable via the 'comments_popup_template' hook.

but it's not correct. The template comes from:

$template = get_query_template( 'comments_popup', array( 'comments-popup.php' ) );

and within get_query_template() we have the following cleaning:

    $type = preg_replace( '|[^a-z0-9-]+|', '', $type );

where the type comments_popup is changed to commentspopup.

The correct filter is therefore:

add_filter( 'commentspopup_template', function( $template )
{   
    return $template;
} );

It looks like the correct type was meant to be comments-popup and not comments_popup. But then we can wonder why the underscore is removed - check out ticket #21213 to read more on that.

I filed a core trac ticket here #32989, regarding the inline documentation.

6
  • thanks a lot for helping out. I tried this <?php comments_popup_script(400, 500); comments_popup_link( 'No comments yet', '1 comment', '% comments', 'comments-link', 'Comments are off for this post'); ?> It opens the popup with url localhost/example/?comments_popup=22 But its blank .....
    – terminator
    Jul 14, 2015 at 7:20
  • Did you override the comments-popup.php file or any PHP errors? @amritanshu
    – birgire
    Jul 14, 2015 at 8:58
  • thanks a lot . But all above you mentioned above is too much for me. Going over my head.. I am going to try it. So i think showing comments in modal overlay is not possible?
    – terminator
    Jul 14, 2015 at 20:59
  • i included this line in my comments-detail.php <?php include("../../../wp-load.php"); ?> And now it works fine :) . Always use this code in ajax popups . Something wrong with this approach???
    – terminator
    Jul 14, 2015 at 21:28
  • Then you're not using what's available in WordPress - No need to include this for ajax or comments-popup. You should let WordPress work better for you ;-) Use the wp_ajax_{$myaction} and wp_ajax_nopriv_{$myaction} hooks instead when working with ajax. Try to copy the /wp-includes/theme-compat/comments-popup.php file into your current theme directory. Here's a live example of the comments popup. @amritanshu
    – birgire
    Jul 15, 2015 at 1:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.