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 badges1 Answer
Reset to default 1You 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' ); } }
本文标签: phpDisplay all comments post not work in Edit comment page
版权声明:本文标题:php - Display all comments post not work in Edit comment page 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741300848a2371080.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论