Removing Admin Bar Node Based on Role

WP_User has a roles array. Get the current user with wp_get_current_user() and check if your role is in the array. add_action( ‘admin_bar_menu’, ‘remove_new_content_menu’, PHP_INT_MAX ); function remove_new_content_menu( $wp_admin_bar ) { // get the current user $user = wp_get_current_user(); // define roles that cannot see the `new content` button $blacklisted_roles = array(‘grocery’, ‘subscriber’); // remove the … Read more

Select dropdown with 2 choices from foreach

You can use in_array to check if the name of the role is either customer or shop_manager add_action(‘register_form’, ‘myplugin_register_form’); function myplugin_register_form() { global $wp_roles; echo ‘<select name=”role” class=”input”>’; foreach ($wp_roles->roles as $key => $value): if ( in_array( $key, array(‘customer’, ‘shop_manager’) ) ) echo ‘<option value=”‘ . $key . ‘”>’ . $value[‘name’] . ‘</option>’; endforeach; echo … Read more

How can I add the ability for a user to add/edit administrators?

Make sure you’re adding and removing roles on plugin activation/deactivation. add_role() only adds a role if it doesn’t already exist. So if you already added the developer role without the capability to add administrators, calling add_role() again won’t re-add the role. You have to remove the role first. <?php /** * Plugin Name: Stackexchange Sample … Read more

Custom site role cannot access Appearance menu

WP Custom Site Roles appear to be a bit ‘flaky’ at best but this particular solution worked for me (created a custom plugin for it). $result = add_role( ‘org_site_owner’, __( ‘Site Owner’ ), array( ‘activate_plugins’ => false, ‘create_users’ => true, ‘customize’ => true, ‘delete_others_pages’ => true, ‘delete_others_posts’ => true, ‘delete_pages’ => true, ‘delete_plugins’ => false, … Read more