How to write conditions on capabilities not on user role?
-
2Capabilities are far easier to test than user roles. How were you testing user roles?– RupCommented May 18, 2021 at 9:24
-
I was implementing that in a plugin and was getting error, added if(!function_exists('wp_get_current_user')) { include(ABSPATH . "wp-includes/pluggable.php"); } above that and it worked– RickyCommented May 27, 2021 at 9:17
Add a comment
|
1 Answer
With current_user_can(), or $user->has_cap() for other users:.
if ( ! current_user_can( 'delete_post', $post_id ) ) {
wp_die( __( 'Sorry, you are not allowed to move this item to the Trash.' ) );
}