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.