44

function change_role_name() { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); //You can list all currently available roles like this... //$roles = $wp_roles->get_names(); //print_r($roles); //You can replace "administrator" with any other role "editor", "author", "contributor" or "subscriber"... ...


38

To hide a post type menu item from non-admin users: function wpse28782_remove_menu_items() { if( !current_user_can( 'administrator' ) ): remove_menu_page( 'edit.php?post_type=your_post_type' ); endif; } add_action( 'admin_menu', 'wpse28782_remove_menu_items' ); your_post_type should be the name of your actual post type. EDIT- other menu ...


37

After a quick chat with Magicroundabout who pointed out a useful resource from Justin Tadlock, it turns out that capabilities for custom post types don't actually exist unless you use add_cap to the role, for example for the following custom post type: add_action( 'init', 'register_cpt_gallery' ); function register_cpt_gallery() { $labels = array( '...


33

Try this... This should work. <?php add_action('init', 'cloneRole'); function cloneRole() { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); $adm = $wp_roles->get_role('administrator'); //Adding a 'new_role' with all admin caps $wp_roles->add_role('new_role', 'My Custom Role', $adm->...


19

It didn't say in the API docs on the Yoast SEO plugin site what the ID was and I don't have a copy of Yoast at installed at disposal, but according to yoas-plugin-dir/admin/class-metabox.php line 144, the meta_box registered is; add_meta_box( 'wpseo_meta', ...etc ); ... Which is hooked onto add_meta_boxes hook on line 32 of the same file, add_action( '...


18

If you don't want to install a plugin to just to do this one task, you can call the WordPress function directly to rebuild the roles and their capabilities. if ( !function_exists( 'populate_roles' ) ) { require_once( ABSPATH . 'wp-admin/includes/schema.php' ); } populate_roles();


17

For a plugin I'm currently working on, I wanted to grant/restrict access to the plugin settings (i.e., the according admin menu pages) on a per role base. Therefore, I had to add a new plugin-specific capability to the user roles. Unfortunately, kaiser's answer seems to be not working anymore, so I spent some time trying to figure out how to allow for the ...


16

This is a simple hack I wrote in a similar situation. It will display all the Subscribers in the Author dropdown on edit/add post/page, from where you can select any one you want. I think it should work for you... add_filter('wp_dropdown_users', 'MySwitchUser'); function MySwitchUser($output) { //global $post is available here, hence you can check for ...


13

You want the admin_menu hook, rather than admin_init. Also, you shouldn't use anonymous functions. Instead, use: function wpse51004_add_menu_page() { add_menu_page('Some Page', 'Some Page', 'manage_options', 'some-slug', 'wpse51004_some_page_callback'); }; add_action('admin_menu', 'wpse51004_add_menu_page'); function wpse51004_some_page_callback() { ...


13

There may be some different way to do that, but most proper way to do that is following. <?php $args = array( 'role' => 'Your desired role goes here.', 'orderby' => 'user_nicename', 'order' => 'ASC' ); $users = get_users( $args ); echo '<ul>'; foreach ( $users as $user ) { echo '<li>' . esc_html( $user->...


12

You cannot assign capabilities to unknown users. If you want to make a post visible for everyone, create a separate URL for these posts and add a control element to the post editor to enable the preview on selected posts only. When such an URL is called, check if a preview is allowed for the post and if the post hasn’t been published already. Also make sure ...


11

As of WordPress 4.4.0 you can now use the wp_dropdown_users_args filter. The code is much simpler now: add_filter( 'wp_dropdown_users_args', 'add_subscribers_to_dropdown', 10, 2 ); function add_subscribers_to_dropdown( $query_args, $r ) { $query_args['who'] = ''; return $query_args; }


10

Here's a mod to MikeSchinkel's answer that checks if the current user has a role of administrator and if not it only selects users that are subscribers. add_action('pre_user_query','yoursite_pre_user_query'); function yoursite_pre_user_query($user_search) { $user = wp_get_current_user(); if ( $user->roles[0] != 'administrator' ) { ...


10

Remove what you add First, please make sure that everything you add on activation also gets removed on uninstall. I got a short tutorial including example code for you. Test with a small plugin: I really don't know much about MU, but as far as I can tell, the roles object is global across all blogs. Just try this little plugin and see what you can get: &...


10

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 ...


10

If you have the unfiltered_html capability then you can use JS. Admins and editors have this capability by default. Personally I use a plugin for fine control of my users' capabilities, but you can make this change easily in code: $role = get_role( 'administrator' ); $role->remove_cap( 'unfiltered_html' ); $role = get_role( 'editor' ); $role-&...


9

I explored WordPress to find difference between it and in schema.php file i found the following function only where in WordPress add_users capability is used. /** * Create and modify WordPress roles for WordPress 3.0. * * @since 3.0.0 */ function populate_roles_300() { $role =& get_role( 'administrator' ); if ( !empty( $role ) ) { $role->...


9

Okay here we go. It is a bug in WordPress itself. I have already shortly explained the issue in my question, so either have a look at it or check out the ticket linked above for more details. Until the issue is properly resolved I propose this dirty, dirty hack. It is based off the idea that as soon as another sub-menu that is accessible is added ...


8

The string below actually represents an array in serialized form. a:1:{s:13:"administrator";s:1:"1";} Here: a:1 means an array with a single element s:13 means string and the length of the string followed by Array { "administrator" => "1" } Once that is in the table, you can use the unserialize() function to return it to an array for use in your ...


8

Looks like a CPT with custom capability_type won't show up by default even as super admin. Even manually adding the permissions for the desired user role (licensing-admin in my case) didn't make it happen - I had to manually add the capabilities for the administrator as well, then everything fell into place. Stuck this code in my plugin activation hook and ...


8

The wp_capabilities saves the value as serilized array, you can try it in your php or for this example here: http://blog.tanist.co.uk/files/unserialize/. The Code: a:1:{s:13:"administrator";b:1;} Is: Array ( [administrator] => 1 ) Meaning the user is an administrator. You can add new roles to the database by running the function add_role, and ...


8

Add: map_meta_cap => true to your $args array. Look here, for more. Hope it helps!


8

If I understand well, user having a special role in your site should: Be able to edit own posts in all statuses but 'publish' and not be able to publish them, just send for revision Be able to edit others posts only when pending, but not be able to publish them, just send for revision Never be able to delete other posts, no matter the status If so, it ...


7

Despite of being ~7 years old, this thread can be googled easily and still provides a working solution. I mean the code provided by @John P Bloch. That said, under PHP 7 it produces a non-critical error (PHP Deprecated) as follows: PHP Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; ...


7

Editing super admin capabilities is a little different from editing the capabilities of every other role, because there is a slight detour in the way WP checks the current user's capabilities. This is what you find on capabilities.php on line 864: function has_cap( $cap ) { // (...) // Multisite super admin has all caps by definition, Unless ...


7

The only prerequisite for current_user_can() is an existing wp_get_current_user(). The latter is defined in pluggable.php, so you can use it after plugins_loaded. The _doing_it_wrong() call you are citing in your question is wrong for itself. My guess is that you took that from BuddyPress or bbPress. Both are running into a recursion if they don't wait that ...


7

It is hard to come up with precise answer since capabilities are often used more broadly than they imply. For example check for manage_options is usually synonym to check for admin user and can come up in contexts that don't actually have much to do with options. Usually it will be a difference between subject content passing or not passing through wp_kses()...


7

The difference is really no difference in regular (single install) WordPress. It's in a multisite install (network) where there is a difference. In multisite, only a Super Admin (who can manage everything on the network) has delete_users capability, while an "admin" (who would own/manage a single site) can remove_users from their site, but cannot delete ...


6

Here's a solution that worked for me: http://thereforei.am/2011/03/15/how-to-allow-administrators-to-edit-users-in-a-wordpress-network/ Pretty elegant and doesn't involve messing with core.


6

Although this isn't well documented, "Super Admin" is not a role (in that it is not an actual role object). It's more like a special "status". A list of users who are Super Admins (also called "network admins" or "site admins") are stored in a database site-option record called site_admins. Generally, adding a capability to the Administrator role is enough ...


Only top voted, non community-wiki answers of a minimum length are eligible