Create custom user role (client) that can create another custom user role (employee) of that client

Clients get added by admins, clients have a parent child relationship with employees which makes filtering easy. So all we need to do is remove anything that doesn’t have to do with employees and filter for employees with a certain meta value. First thing’s first, whenever a new user is registered on the admin side … Read more

Temporary capability for current_user_can()

Yes, just filter ‘user_has_cap’. You get an array with the current capabilities that you can change without touching the database. Sample Code add_filter( ‘user_has_cap’, ‘wpse_53230_catch_cap’, 10, 3 ); /** * See WP_User::has_cap() in wp-includes/capabilities.php * * @param array $allcaps Existing capabilities for the user * @param string $caps Capabilities provided by map_meta_cap() * @param array … Read more

wp_update_user not updating

Im using remove_role and then add_role to upgrade a user from one role to another. Here is a function that checks all user within the user role subscriber and upgrade them to editor every hour. /** * Add a cron job that will update * Subscribers to editors. Runs hourly */ add_action(‘check_user_role’, ‘upgrade_user’); function run_check_user_role() … Read more

How do I programmatically set default role for new users?

This allows plugins to easily hijack the default role while they’re active. // Hijack the option, the role will follow! add_filter(‘pre_option_default_role’, function($default_role){ // You can also add conditional tags here and return whatever return ‘subscriber’; // This is changed return $default_role; // This allows default }); I use it to make sure some plugins that … Read more

How to allow editor to edit privacy page / settings only?

Editing the privacy policy page is restricted to manage_privacy_options as pointed out in a comment in the WordPress core file wp-includes/capabilities.php: /* * Setting the privacy policy page requires `manage_privacy_options`, * so editing it should require that too. */ if ( (int) get_option( ‘wp_page_for_privacy_policy’ ) === $post->ID ) { $caps = array_merge( $caps, map_meta_cap( ‘manage_privacy_options’, … Read more

How to get role of user

Use the user ID and WP_User: $user = new WP_User( $user_id ); print wp_sprintf_l( ‘%l’, $user->roles ); Update /** * Get user roles by user ID. * * @param int $id * @return array */ function wpse_58916_user_roles_by_id( $id ) { $user = new WP_User( $id ); if ( empty ( $user->roles ) or ! is_array( … Read more