admin管理员组文章数量:1122832
I am running into a problem in that I have users that can edit comments, but only those placed onto their own posts. Which is what I want. Unfortunately the edit-comments.php
page shows all comments. Is there a way to hide/remove the comments that the user can't manage anyway?
I tried:
function my_plugin_get_comment_list_by_user($clauses) {
if (is_admin()) {
global $user_ID, $wpdb;
$clauses['join'] = ", wp_posts";
$clauses['where'] .= " AND wp_posts.post_author = ".$user_ID." AND wp_commentsment_post_ID = wp_posts.ID";
}
return $clauses;
}
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}
But this has no effect
I am running into a problem in that I have users that can edit comments, but only those placed onto their own posts. Which is what I want. Unfortunately the edit-comments.php
page shows all comments. Is there a way to hide/remove the comments that the user can't manage anyway?
I tried:
function my_plugin_get_comment_list_by_user($clauses) {
if (is_admin()) {
global $user_ID, $wpdb;
$clauses['join'] = ", wp_posts";
$clauses['where'] .= " AND wp_posts.post_author = ".$user_ID." AND wp_comments.comment_post_ID = wp_posts.ID";
}
return $clauses;
}
if(!current_user_can('edit_others_posts')) {
add_filter('comments_clauses', 'my_plugin_get_comment_list_by_user');
}
But this has no effect
Share Improve this question edited Apr 10, 2024 at 12:03 Klein asked Apr 10, 2024 at 8:08 KleinKlein 114 bronze badges 3 |2 Answers
Reset to default 0I found another piece of code on the internet which I have modified to work.
add_filter('the_comments', 'edit_comments_filter_comments');
function edit_comments_filter_comments($comments){
global $pagenow;
$currentuserid = get_current_user_id();
if($pagenow == 'edit-comments.php' && !current_user_can('edit_others_posts')){
foreach($comments as $i => $comment){
$the_post = get_post($comment->comment_post_ID);
if($comment->user_id != $currentuserid && $the_post->post_author != $currentuserid)
unset($comments[$i]);
}
}
return $comments;
}
As I understand it, this takes the comments pulled in by the query and checks them before displaying them.
Instead of modifying raw SQL it's easier to add a parameter to the comment query combined with a condition in pre_get_comments
:
https://developer.wordpress.org/reference/hooks/pre_get_comments/
E.g. this filter modifies the comment query in the admin area to only show comments on posts that belong to you:
add_action( 'pre_get_comments', 'only_your_posts_comments_in_admin' );
function only_your_posts_comments_in_admin( WP_Comment_Query $query ) : void {
if ( ! is_admin() ) {
return; // we only want to change admin screens
}
// limit to just the comments I made:
$query->set( 'user_id', get_current_user_id() );
}
You can then modify that function to check if the user can/can't modify other peoples posts via current_user_can
.
本文标签: dashboardHide comments from admin commentsphp that user can39t edit or manage
版权声明:本文标题:dashboard - Hide comments from admin comments.php that user can't edit or manage 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1736310896a1934543.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
post_author__in
, you don't need to mess with the raw SQL to do that when we already havepre_get_comments
andWP_Comment_Query
. It would also be much more afficient to move thecurrent_user_can
check into the function so that it isn't broken by being super early or modifications made to the user state. It would also be better to fetch the current user ID using the proper function rather than bypassing it and using a global variable developer.wordpress.org/reference/functions/get_current_user_id – Tom J Nowell ♦ Commented Apr 10, 2024 at 8:46