Display all comments post not work in Edit comment page

You need to do these, in order to make the “Show comments” part works properly:

  1. On the “Edit Comment” page (at wp-admin/comment.php), the first parameter passed to the meta box callback (your my_post_comment_meta_box() function) is a WP_Comment object and not a WP_Post object, so you need to manually define $post in your function.

  2. Add back the get-comments nonce to your function, just like in the original post_comment_meta_box() function.

  3. After that, echo a hidden input with post_ID as the input id and the comment’s post ID as the value.

  4. The meta box uses a global JavaScript variable/object named commentsBox that’s defined in wp-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:

  1. In the above my_admin_enqueue_scripts() function, add the following after the wp_enqueue_script( 'post' ); :

    // Enqueue the comment reply script.
    wp_enqueue_script( 'admin-comments' );
    
  2. 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' );
        }
    }
    

Leave a Comment