Check if specific role exists

This is based on Bosco’s comment. You can do this instead of line 3 in the above code: wp_roles()->is_role( ‘editor’ ); That grabs a WP_Roles object (what the code you had tries to do by getting a global variable) and calls the same is_role() function.

Disable Woocommerce checkout based on user role [closed]

Woocommerce is considered off-topic here but as a general answer, it is considered best practice to check against capabilities instead of roles In WP, this is done like that: add_action(‘init’, ‘wpse_capability_check’); // hooked on ‘init’ but depends on your actual code logic function wpse_capability_check(){ // you should check on a capability that only validated user … Read more

How to add user roles? [closed]

What you want is add_role() The Codex provides sample code: $result = add_role( ‘basic_contributor’, __( ‘Basic Contributor’ ), array( ‘read’ => true, // true allows this capability ‘edit_posts’ => true, ‘delete_posts’ => false, // Use false to explicitly deny ) ); if ( null !== $result ) { echo ‘Yay! New role created!’; } else … Read more

I want this code to work only for Authors AND Contributors

Here you go, and remember, this assumes that your function was already working (I did not check that it was), and if it was, then just wrap like this: <?php add_action( ‘admin_menu’, ‘wf_cli_remove_admin_menu’ ); function wf_cli_remove_admin_menu() { $wf_user = wp_get_current_user(); $wf_roles = array( ‘contributor’, ‘author’ ); if( array_intersect( $wf_roles, $wf_user->roles ) ) : remove_menu_page( ‘index.php’ … Read more