How to programmatically add a user to a role?
You can do that like this : $my_user = new WP_User( $user_id ); $my_user->set_role( “editor” );
You can do that like this : $my_user = new WP_User( $user_id ); $my_user->set_role( “editor” );
There is apparently an open ticket about this bug. Here is a workaround that I tested: $args = array( ‘meta_key’ => ‘last_name’, ‘role’ => ‘guest-teacher’ ); $wp_user_query = new WP_User_Query($args); $wp_user_query->query_orderby = str_replace( ‘user_login’, ‘wp_usermeta.meta_value’, $wp_user_query->query_orderby ); $wp_user_query->query(); $authors = $wp_user_query->get_results(); Problem with this is that it runs the query twice.
Arrrrg a WooCommerce question… quick burn him at the stake! There’s a couple of problems with your callback function above: you are not declaring global $woocommerce so you do not have access to the $woocommerce global variable. and $woocommerce->customer holds the state of the WC_Customer class however this class does not contain a get_role() method, … Read more
I’ve found that the WordPress has_cap() function, which is relied on by functions like user_can() and current_user_can, explicitly returns false for empty capabilities. Example: If a capability is passed as an argument using current_user_can(), this function will pass the capability to has_cap() and return the results: return call_user_func_array( array( $current_user, ‘has_cap’ ), $args has_cap() will … Read more
Just to clarify: You have, for example, the categories ‘foo’, ‘bar’ and ‘baz’. New users with the role ‘foo’ can only read posts in the category ‘foo’. After they have read all the posts in ‘foo’, their role will be changed to ‘bar’ so they can read the posts in category ‘bar’. And so on… … Read more
Unfortunately, WordPress doesn’t use a separate capability for previewing posts. A user needs the ability to edit a post in order to preview it. As seen in the WP_Query::get_posts() method: // User must have edit permissions on the draft to preview. if ( ! current_user_can($edit_cap, $this->posts[0]->ID) ) { $this->posts = array(); } What you could … Read more
EDIT I updated the function to check for role for which we want to make changes for, if the current user role is not within our roles_to_change array then the function exits. Note that I haven’t tested for bugs, so if you find any, just report them here. @Malisa’s answer is a good approach. However, … Read more
Put this into your functions.php file in your theme folder… function query_set_only_author( $wp_query ) { global $current_user; if ( is_admin() && !current_user_can(‘manage_options’) ) { $wp_query->set( ‘author’, $current_user->ID ); } } add_action(‘pre_get_posts’, ‘query_set_only_author’ );
How about using get_users()? You probably don’t even need parameters for it, default behavior should be just what you’re looking for.
The UI select element On user-edit.php, you see the drop-down in the UI. The drop down <select> wrapper is hard coded. Then the admin interface does a nifty thing 1) according to the inline comment: // Get the highest/primary role for this user. In fact it is getting the first role, that was assigned to … Read more