Have some problem... Maybe somebody knows how to set role for current user in Wordpress.
I tried this code
function uproles()
{
$current_user = wp_get_current_user();
$roles = $current_user->roles;
if (current_user_can('access1') == true) {$role = "access2";}
do_action('set_user_role', $current_user->ID, $role, $roles );
}
But it seems it does not work:(
From admin panel Just goto Users > your user and set the user role to what ever you want. If you want to set a default role to all new users, goto Settings>writing there is a field where you can set the default role.
Related
I created a new user role, but I need this user role to only be able to edit users with another user role, I do not want this new user role to be able to edit users like, editors, author, etc...
Ideas?
you can add roles so:
$result = add_role(
'editor_of_users',
__( 'Editor of Users' ),
array(
'edit_users' => true,
// Add more capabilities...
)
);
if ( null !== $result ) {
echo 'Yay! New role created!';
}
else {
echo 'Oh... the editor_of_users role already exists.';
}
For more information you can read the documentation about: add_role
And the all users capabilities here: Roles and Capabilities
EDIT:
Read this post: to know how add to users the capabilities to edit specific roles...
How to allow an user role to create a new user under a role which lower than his level only?
I hope you resolve it... Good Look! ;)
Iam working with buddypress,
I have a two user roles,
1-student
2-faculty
and i have set default user role as subscriber.
when user registers and activates account by clicking on link sent through mail.User role changes to default(subscriber).
Any idea what is the issue? Below is the code assigning role to user on sign up.
add_action('bp_core_signup_user', 'ad_user_signup_usermeta', 10, 5);
function ad_user_signup_usermeta($user_id, $user_login, $user_password, $user_email, $usermeta) {
if(isset($_POST['signup_membership']) && !empty($_POST['signup_membership']))
update_user_meta($user_id, 'membership', $_POST['signup_membership']);
$userdata = array();
$userdata['ID'] = $user_id;
if(!empty($_POST['signup_usertype'])) {
if($_POST['signup_usertype'] == 'student') {
$userdata['role'] = 'student';
}
if($_POST['signup_usertype'] == 'instructor') {
$userdata['role'] = 'instructor';
}
}
if ($userdata['role']){
wp_update_user($userdata);
}
}
Upon activation, BuddyPress (at least version 2.0.2) updates the user's role to the default role.
https://buddypress.trac.wordpress.org/browser/tags/2.0.2/bp-members/bp-members-functions.php#L1560
You can comment out that line, or write some code to work around it. I'm using "WP Roles At Registration" and ran across the same problem. I ended up adding a filter on bp_core_signup_user to save the original role but you'll want to add something like this to your ad_user_signup_usermeta:
update_user_meta($user_id, 'temp_role', $role_name)
then reset it back in a filter for bp_core_activated_user
public function after_bp_activated_user($user_id, $key, $user) {
$user = get_userdata($user_id);
$role = get_user_meta($user_id, 'temp_role');
if ($role) {
$user->set_role($role[0]);
}
}
add_filter('bp_core_activated_user', array($this, 'after_bp_activated_user'), 30, 3);
I need to grant users a specific role (Editor, Administrator, etc.) along with all its capabilities on the fly in Wordpress, but I don't want to update their role in the database (so that the next time they come back, they will have their original role). How can I go about doing this?
Here's what I ended up doing:
add_filter( 'user_has_cap', 'override_caps' );
function override_caps($allcaps){
if( ... ){ // When to override caps
$role_name = 'administrator';
$role = get_role($role_name); // Get the role object by role name
$allcaps = $role->capabilities; // Get the capabilities for the role
$allcaps[$role_name] = true; // Add role name to capabilities
}
return $allcaps;
}
I am using wp_create_user function to register new user in wordpress.
This function is inserting user as a role of subscriber. I want to insert users as a author type role. How can i do this.?
Thanks in advance.
The wp_create_user() function returns the ID of the user it creates. You can then use the WP_User class like:
$new_user_id = wp_create_user($your_args_here);
$u = new WP_User( $new_user_id );
// Remove role
$u->remove_role( 'subscriber' );
// Add role
$u->add_role( 'author' );
Alternatively, you can use wp_update_user() function:
Although I'm not precisely sure how to do that, but I imagine you could work it out by looking in wp-includes/registration.php which is where that function is created.
While a user is creating a new post, how do I determine his current role?
I'm assuming you know what hooks of Wordpress you want to use. So skipping that part, it's pretty easy to get the current role of the user
$current_user = wp_get_current_user();
if ( !($current_user instanceof WP_User) )
return;
$roles = $current_user->roles; //$roles is an array
Now, you can iterate over that array to see if the user has a particular role.
Or, you can use current_user_can to look for specific capabilities, if you just want to check whether or not a user has a specific permission versus whether or not they're in the role. For example:
if (current_user_can('delete_posts')) {
//display the delete posts button.
}
This code will help you
<?php echo array_shift(wp_get_current_user()->roles); ?>