首先,请注意您的代码:如果您没有用花括号封装条件的代码:if($something){ encapsulated_actions(); }
它将只执行下一行。
因此,您应该使用:
if( \'post\' != get_post_type() ) // if different of desired type, do nothing and return default
return $actions;
// do your thing and return modified var
unset ($actions[\'spam\'], $actions[\'trash\'] );
return $actions;
或:
if( \'post\' == get_post_type())
{
unset ($actions[\'spam\']);
unset ($actions[\'trash\']);
/* The following is best use */
// unset ($actions[\'spam\'], $actions[\'trash\'] );
}
// returns modified or unmodified var depending on previous check
return $actions;
我们需要针对四个地方来阻止/隐藏对评论的诽谤。在这里,我正在检查管理角色。要检查特定用户,请使用
get_current_user_id()
.
函数名表示WordPress答案中原始问题的ID,例如。,wpse_92313
是:Comments screen in backend, how to disable Quick Edit | Edit | History | Spam | for non admins
1) 快速编辑操作行
add_filter( \'comment_row_actions\', \'comments_row_wpse_92313\', 15, 2 );
function comments_row_wpse_92313( $actions, $comment )
{
/* Type of the parent post */
// $parent_type = get_post_type( $comment->comment_post_ID ) );
if( !current_user_can( \'delete_plugins\' ) )
unset($actions[\'trash\'] );
return $actions;
}
视图行(
All | Pending | ... | Trash
)
add_filter( \'views_edit-comments\', \'wpse_30331_custom_view_count\', 10, 1);
function wpse_30331_custom_view_count( $views )
{
if( !current_user_can( \'delete_plugins\' ) )
unset( $views[\'trash\'] );
return $views;
}
批量操作我刚刚发现了过滤器挂钩
\'bulk_actions-\' . $this->screen->id
add_filter( \'bulk_actions-edit-comments\', \'remove_bulk_trash_wpse_92534\' );
function remove_bulk_trash_wpse_92534( $actions )
{
if( !current_user_can( \'delete_plugins\' ) )
unset( $actions[\'trash\'] );
return $actions;
}
阻止直接访问垃圾屏幕重定向
example.com/wp-admin/edit-comments.php?comment_status=trash
至主评论屏幕。
add_action( \'admin_head-edit-comments.php\', \'wpse_74488_block_trash_access\' );
function wpse_74488_block_trash_access()
{
// Don\'t run the function for Admins
if( current_user_can( \'delete_plugins\' ) )
return;
if( isset( $_GET[\'comment_status\'] ) && \'trash\' == $_GET[\'comment_status\'] )
{
wp_redirect( admin_url( \'edit-comments.php\' ) );
exit;
}
}