You need to do these, in order to make the “Show comments” part works properly:
-
On the “Edit Comment” page (at
wp-admin/comment.php
), the first parameter passed to the meta box callback (yourmy_post_comment_meta_box()
function) is aWP_Comment
object and not aWP_Post
object, so you need to manually define$post
in your function. -
Add back the
get-comments
nonce to your function, just like in the originalpost_comment_meta_box()
function. -
After that, echo a hidden input with
post_ID
as the inputid
and the comment’s post ID as the value. -
The meta box uses a global JavaScript variable/object named
commentsBox
that’s defined inwp-admin/js/post.js
, so you need to load that script on the page.
So your function should begin like this:
function my_post_comment_meta_box( $comment ) { // use $comment and not $post
$post = get_post( $comment->comment_post_ID ); // manually define $post
// Add the post ID input.
echo '<input type="hidden" id="post_ID" value="' . $post->ID . '">';
// Then the AJAX nonce.
wp_nonce_field( 'get-comments', 'add_comment_nonce', false );
And then you can use the admin_enqueue_scripts
hook to load the post.js
script:
add_action( 'admin_enqueue_scripts', 'my_admin_enqueue_scripts' );
function my_admin_enqueue_scripts() {
// Enqueue the script only if the current page is comment.php
if ( 'comment' === get_current_screen()->id ) {
wp_enqueue_script( 'post' );
}
}
Update
If you want the comment’s action links such as “Reply” and “Quick Edit” to work, then:
-
In the above
my_admin_enqueue_scripts()
function, add the following after thewp_enqueue_script( 'post' );
:// Enqueue the comment reply script. wp_enqueue_script( 'admin-comments' );
-
Then after that function, add this which outputs the inline comment reply form: (We have to put it in the header so that the
action
input is not overwritten. There’s a jQuery/JS way to overcome the issue, but I’d just use the following, and don’t worry, the form is hidden by default.)add_action( 'in_admin_header', 'my_in_admin_header' ); function my_in_admin_header() { if ( 'comment' === get_current_screen()->id ) { wp_comment_reply( '-1' ); } }