Hot answers tagged

31

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


28

Roles are stored in the global variable $wp_roles. The ideal function is get_editable_roles() from /wp-admin/includes/user.php function get_editable_roles() { global $wp_roles; $all_roles = $wp_roles->roles; $editable_roles = apply_filters('editable_roles', $all_roles); return $editable_roles; } The "editable" part is because it ...


27

It's actually pretty easy. You need to filter into map_meta_caps and stop editors from creating/editing admins, and remove the administrator role from the 'editable roles' array. This class, as a plugin or in your theme's functions.php file would do it: class JPB_User_Caps { // Add our filters function JPB_User_Caps(){ add_filter( 'editable_roles', ...


19

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


18

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


17

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


16

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


13

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

When assigning capabilities to 'capabilities' argument of register_taxonomy() you need to assign the capability and not the role! so use capabilities that only a specific role has eg: 'capabilities' => array ( 'manage_terms' => 'manage_options', //by default only admin 'edit_terms' => 'manage_options', ...


12

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


10

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


9

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() { ...


9

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' ) { ...


9

Hi @Carlos: Try adding the following to your theme's functions.php file, or in a .php file within a plugin that you might be writing (which works for WordPress 3.1.x): add_action('pre_user_query','yoursite_pre_user_query'); function yoursite_pre_user_query($user_search) { $user = wp_get_current_user(); if ($user->ID!=1) { // Is not administrator, ...


9

You code seems to be correct. Try the following instead. $args = array( 'labels' => $labels, 'public' => true, 'publicly_queryable' => true, 'show_ui' => true, 'query_var' => true, 'rewrite' => true, 'hierarchical' => false, 'menu_position' => null, 'supports' => array('title'), ...


8

Try this: function get_role_names() { global $wp_roles; if ( ! isset( $wp_roles ) ) $wp_roles = new WP_Roles(); return $wp_roles->get_names(); } PS heh, missed that explanation and reply, too fast me :)


8

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


8

Hi @NetConstructor: I think this is what you need. Note that I didn't include the full setup of your 'website_owner' role, just the addition of a new capability called 'manage_administrators'. Also, I only attempted to remove the "Delete" link from any users that don't have the 'manage_administrators' capability (which you'll need to add to the ...


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


7

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


7

You can use WP_Role class, // get the the role object $role_object = get_role( $role_name ); // add $cap capability to this role object $role_object->add_cap( $capability_name ); // remove $cap capability from this role object $role_object->remove_cap( $capability_name ); or you can run this once in your functions: /* Roles & Capabilities */ ...


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


6

Me, like @fischi think that filter 'user_has_cap' is the best choiche for the pourpose, however, I think that is better to the work, regardless the $_GET post or action: WordPress check the meta cabability on a per-post basis, using an additional argument. In few words, when filtering 'user_has_cap' for a meta capability (see ...


6

The register_post_type has a capabilities (or simple version: capability_type) that you can use to specify a separate capability to create and edit posts of this type. If you set this, you can create a new role that has the capability to edit these custom posts, but not regular posts.


6

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


6

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


5

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


5

You can remove it using the remove_meta_box function. if ( ! current_user_can( 'edit_pages' ) ) { add_action( 'add_meta_boxes', 'my_remove_wp_seo_meta_box', 100000 ); } function my_remove_wp_seo_meta_box() { remove_meta_box( 'wpseo_meta', 'post', 'normal' ); } Notice the 100000 in the add_action, this makes sure that this is done after the WP SEO ...


5

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


5

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



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