admin管理员组

文章数量:1279209

I am trying to show all comments related to the post the comment being edited belongs to.

My code (based on this example)

add_action( 'admin_menu', 'all_display_comments_add_meta_box' );

function all_display_comments_add_meta_box() {
    add_meta_box( 'commentsdiv', __( 'Comments' ), 'my_post_comment_meta_box', 'comment', 'normal', 'high' );
}

function my_post_comment_meta_box( $post ) {

    $total         = get_comments(
        array(
            'post_id' => $post->ID,
            'number'  => 1,
            'count'   => true,
        )
    );
    $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' );
    $wp_list_table->display( true );

    if ( 1 > $total ) {
        echo '<p id="no-comments">' . __( 'No comments yet.' ) . '</p>';
    } else {
        $hidden = get_hidden_meta_boxes( get_current_screen() );
        if ( ! in_array( 'commentsdiv', $hidden, true ) ) {
            ?>
            <script type="text/javascript">jQuery(document).ready(function(){commentsBox.get(<?php echo $total; ?>, 10);});</script>
            <?php
        }

        ?>
        <p class="hide-if-no-js" id="show-comments"><a href="#commentstatusdiv" onclick="commentsBox.load(<?php echo $total; ?>);return false;"><?php _e( 'Show comments' ); ?></a> <span class="spinner"></span></p>
        <?php
    }

    wp_comment_trashnotice();
}

Sorry for my English.

I am trying to show all comments related to the post the comment being edited belongs to.

My code (based on this example)

add_action( 'admin_menu', 'all_display_comments_add_meta_box' );

function all_display_comments_add_meta_box() {
    add_meta_box( 'commentsdiv', __( 'Comments' ), 'my_post_comment_meta_box', 'comment', 'normal', 'high' );
}

function my_post_comment_meta_box( $post ) {

    $total         = get_comments(
        array(
            'post_id' => $post->ID,
            'number'  => 1,
            'count'   => true,
        )
    );
    $wp_list_table = _get_list_table( 'WP_Post_Comments_List_Table' );
    $wp_list_table->display( true );

    if ( 1 > $total ) {
        echo '<p id="no-comments">' . __( 'No comments yet.' ) . '</p>';
    } else {
        $hidden = get_hidden_meta_boxes( get_current_screen() );
        if ( ! in_array( 'commentsdiv', $hidden, true ) ) {
            ?>
            <script type="text/javascript">jQuery(document).ready(function(){commentsBox.get(<?php echo $total; ?>, 10);});</script>
            <?php
        }

        ?>
        <p class="hide-if-no-js" id="show-comments"><a href="#commentstatusdiv" onclick="commentsBox.load(<?php echo $total; ?>);return false;"><?php _e( 'Show comments' ); ?></a> <span class="spinner"></span></p>
        <?php
    }

    wp_comment_trashnotice();
}

Sorry for my English.

Share Improve this question edited Sep 23, 2021 at 17:21 UserUser-name asked Sep 23, 2021 at 13:58 UserUser-nameUserUser-name 577 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

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' );
        }
    }
    

本文标签: phpDisplay all comments post not work in Edit comment page