Execute a function when admin changes the user role

You can use the set_user_role hook, that will only fire when the user role changes: add_action( ‘set_user_role’, function( $user_id, $role, $old_roles ) { // Your code … }, 10, 3 ); If you want to restrict this to a profile update, you can use: add_action( ‘set_user_role’, function( $user_id ) { add_action( ‘profile_update’, function( $user_id ) … Read more

Publish author posts only with editor approval?

Add the following code to your functions.php: function allow_contributor_uploads() { $contributor = get_role(‘contributor’); $contributor->add_cap(‘upload_files’); } if ( current_user_can(‘contributor’) && !current_user_can(‘upload_files’) ) { add_action(‘admin_init’, ‘allow_contributor_uploads’); } This will add the upload_files capability to the Contributor role. It only needs to run once; just login to admin as a user with the Contributor role. After it successfully … Read more

Change default admin page for specific role(s)

In your theme’s functions.php: function hide_the_dashboard() { global $current_user; // is there a user ? if ( is_array( $current_user->roles ) ) { // substitute your role(s): if ( in_array( ‘custom_role’, $current_user->roles ) ) { // hide the dashboard: remove_menu_page( ‘index.php’ ); } } } add_action( ‘admin_menu’, ‘hide_the_dashboard’ ); function your_login_redirect( $redirect_to, $request, $user ) { … Read more

Deactivate plugin for a specific user group

I think the answer to this Disable plugin / plugin action via theme is good for base knowledge on how to disable plugins from code. Adapting that knowledge to your needs will leave us with this: add_action(‘admin_init’, ‘my_filter_the_plugins’); function my_filter_the_plugins() { global $current_user; if (in_array(‘media_manager’, $current_user->roles)) { deactivate_plugins( // deactivate for media_manager array( ‘/advanced-page-manager/advanced_page_manager.php’ ), … Read more

Custom user role cannot see or modify featured image

Users that are only assigned to your event_admin role, don’t have the upload_files capability, needed to display the featured image meta box. Here’s the relevant code from the core: if ( $thumbnail_support && current_user_can( ‘upload_files’ ) ) // <– Notice this check add_meta_box( ‘postimagediv’, esc_html( $post_type_object->labels->featured_image ), ‘post_thumbnail_meta_box’, null, ‘side’, ‘low’ ); Note that if … Read more

Why does my custom WP role need edit_posts to edit images?

If I had to guess: because images are Attachments, and Attachments are a Post-Type. Thus, in order to edit an image, which is an attachment, which is a post, requires the edit_post capability. EDIT Don’t you have your capability mapping array keys/values reversed? e.g. you have ‘edit_posts’ => ‘edit_listings’. Shouldn’t it instead be ‘edit_listings’ => … Read more

Query users by custom taxonomy and user role

Apparently there is no core implementation of ‘tax_query’ in WP_User_Query yet. Check the ticket here for more info –> https://core.trac.wordpress.org/ticket/31383 Nevertheless there is an alternative way using get_objects_in_term $taxonomy = ‘shop-category’; $users = get_objects_in_term( $term_id, $taxonomy ); if(!empty($users)){ // WP_User_Query arguments $args = array ( ‘role’ => ‘shop_manager’, ‘order’ => ‘DESC’, ‘orderby’ => ‘user_registered’, ‘include’ … Read more

alphabetically order role drop-down selection in dashboard

Almost the same approach One Trick Pony has chosen, but I am using translated names and uasort() (to preserve the keys): add_filter( ‘editable_roles’, ‘t5_sort_editable_roles’ ); /** * Array of roles. * * @wp-hook editable_roles * @param array $roles * @return array */ function t5_sort_editable_roles( $roles ) { uasort( $roles, ‘t5_uasort_editable_roles’ ); return $roles; } /** … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)