Shold I manually add ‘cap’ to admin role ?

A new capability has to be explicitly added to either a role or a user. In your case if you want all administrators to have ‘cap’ capability you will add it to ‘administrator’ role: $role = get_role( ‘administrator’ ); $role->add_cap( ‘cap’ ); If you want a specific administrator only to have ‘cap’ capability then you … Read more

How-to Delay The Capability To Publish Posts?

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 … Read more

How do I properly format the user_role array?

try : a:5:{ s:13:”administrator”;a:2:{s:4:”name”;s:13:”Administrator”;s:12:”capabilities”;a:54:{s:13:”switch_themes”;b:1;s:11:”edit_themes”;b:1;s:16:”activate_plugins”;b:1;s:12:”edit_plugins”;b:1;s:10:”edit_users”;b:1;s:10:”edit_files”;b:1;s:14:”manage_options”;b:1;s:17:”moderate_comments”;b:1;s:17:”manage_categories”;b:1;s:12:”manage_links”;b:1;s:12:”upload_files”;b:1;s:6:”import”;b:1;s:15:”unfiltered_html”;b:1;s:10:”edit_posts”;b:1;s:17:”edit_others_posts”;b:1;s:20:”edit_published_posts”;b:1;s:13:”publish_posts”;b:1;s:10:”edit_pages”;b:1;s:4:”read”;b:1;s:8:”level_10″;b:1;s:7:”level_9″;b:1;s:7:”level_8″;b:1;s:7:”level_7″;b:1;s:7:”level_6″;b:1;s:7:”level_5″;b:1;s:7:”level_4″;b:1;s:7:”level_3″;b:1;s:7:”level_2″;b:1;s:7:”level_1″;b:1;s:7:”level_0″;b:1;s:17:”edit_others_pages”;b:1;s:20:”edit_published_pages”;b:1;s:13:”publish_pages”;b:1;s:12:”delete_pages”;b:1;s:19:”delete_others_pages”;b:1;s:22:”delete_published_pages”;b:1;s:12:”delete_posts”;b:1;s:19:”delete_others_posts”;b:1;s:22:”delete_published_posts”;b:1;s:20:”delete_private_posts”;b:1;s:18:”edit_private_posts”;b:1;s:18:”read_private_posts”;b:1;s:20:”delete_private_pages”;b:1;s:18:”edit_private_pages”;b:1;s:18:”read_private_pages”;b:1;s:12:”delete_users”;b:1;s:12:”create_users”;b:1;s:17:”unfiltered_upload”;b:1;s:14:”edit_dashboard”;b:1;s:14:”update_plugins”;b:1;s:14:”delete_plugins”;b:1;s:15:”install_plugins”;b:1;s:13:”update_themes”;b:1;s:14:”install_themes”;b:1;}} s:6:”editor”;a:2:{s:4:”name”;s:6:”Editor”;s:12:”capabilities”;a:34:{s:17:”moderate_comments”;b:1;s:17:”manage_categories”;b:1;s:12:”manage_links”;b:1;s:12:”upload_files”;b:1;s:15:”unfiltered_html”;b:1;s:10:”edit_posts”;b:1;s:17:”edit_others_posts”;b:1;s:20:”edit_published_posts”;b:1;s:13:”publish_posts”;b:1;s:10:”edit_pages”;b:1;s:4:”read”;b:1;s:7:”level_7″;b:1;s:7:”level_6″;b:1;s:7:”level_5″;b:1;s:7:”level_4″;b:1;s:7:”level_3″;b:1;s:7:”level_2″;b:1;s:7:”level_1″;b:1;s:7:”level_0″;b:1;s:17:”edit_others_pages”;b:1;s:20:”edit_published_pages”;b:1;s:13:”publish_pages”;b:1;s:12:”delete_pages”;b:1;s:19:”delete_others_pages”;b:1;s:22:”delete_published_pages”;b:1;s:12:”delete_posts”;b:1;s:19:”delete_others_posts”;b:1;s:22:”delete_published_posts”;b:1;s:20:”delete_private_posts”;b:1;s:18:”edit_private_posts”;b:1;s:18:”read_private_posts”;b:1;s:20:”delete_private_pages”;b:1;s:18:”edit_private_pages”;b:1;s:18:”read_private_pages”;b:1;}} s:6:”author”;a:2:{s:4:”name”;s:6:”Author”;s:12:”capabilities”;a:10:{s:12:”upload_files”;b:1;s:10:”edit_posts”;b:1;s:20:”edit_published_posts”;b:1;s:13:”publish_posts”;b:1;s:4:”read”;b:1;s:7:”level_2″;b:1;s:7:”level_1″;b:1;s:7:”level_0″;b:1;s:12:”delete_posts”;b:1;s:22:”delete_published_posts”;b:1;}} s:11:”contributor”;a:2:{s:4:”name”;s:11:”Contributor”;s:12:”capabilities”;a:5:{s:10:”edit_posts”;b:1;s:4:”read”;b:1;s:7:”level_1″;b:1;s:7:”level_0″;b:1;s:12:”delete_posts”;b:1;}} s:10:”subscriber”;a:2:{s:4:”name”;s:10:”Subscriber”;s:12:”capabilities”;a:2:{s:4:”read”;b:1;s:7:”level_0″;b:1;}}} which are the roles and capabilities that are set by default.

Email notification for editors only

Use get_users() function. Reference function notify_editors( $post_id ) { $post = get_post( $post_id ); // Get all editors $editors = get_users( [ ‘role__in’ => [ ‘editor’] ] ); foreach ( $editors as $editor ) { // Setup email $subject = “Post Published: ” . $post->post_title; $message=” Hi ” . $editor->display_name . ‘, Your post, “‘ … Read more

Woocommerce: change user role after completing order

Looking through the code there is a very relevant example: function woocommerce_paying_customer( $order_id ) { $order = new WC_Order( $order_id ); if ( $order->user_id > 0 ) { $old_spent = absint( get_user_meta( $order->user_id, ‘_money_spent’, true ) ); update_user_meta( $order->user_id, ‘_money_spent’, $old_spent + $order->order_total ); $old_count = absint( get_user_meta( $order->user_id, ‘_order_count’, true ) ); update_user_meta( $order->user_id, … Read more

Displaying which Role the current user is assigned to

As you suggested, here’s how you can display the roles next to the username in the admin bar: function wpse_203917_admin_bar_menu( $wp_admin_bar ) { if ( ! $node = $wp_admin_bar->get_node( ‘my-account’ ) ) return; $roles = wp_get_current_user()->roles; $node->title .= sprintf( ‘ (%s)’, implode( ‘, ‘, $roles ) ); $wp_admin_bar->add_node( $node ); } add_action( ‘admin_bar_menu’, ‘wpse_203917_admin_bar_menu’ );

WordPress edit_user_profile_update update secondary role

I have finally found solution and the right hook for this: profile_update is called at the end of wp_insert_user in user.php which is called from edit_user. wp_insert_user ending: if ( $update ) do_action(‘profile_update’, $user_id, $old_user_data); else do_action(‘user_register’, $user_id); return $user_id;

Customize Admin Users Screen based on Role

Manage Columns It’s pretty straight forward using the manage_{post-type-name}_columns filter: Just switch per $capability and unset what you don’t need in the $post_columns array. function wpse19435_manage_columns( $posts_columns ) { // First role: add a column – take a look at the second function if ( current_user_can( $capability_admin ) ) { $posts_columns[‘some_column_name’] = _x( ‘Whatever’, ‘column … Read more

List total number of users that are authors

count_users() should give you an array of all the required user counts. You can use it like this. $user_counts = count_users(); $authors = $user_counts[‘avail_roles’][‘author’]; //Get the author count $subscribers = $user_counts[‘avail_roles’][‘subscriber’]; //Get the subscriber count echo $authors. ‘ Authors so far’; echo $subscribers. ‘ Subscribers so far’;

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