Assign role to user on first login, if they are first (after admin)

To set is the connected user belongs to the blog, you need to do that on hook wp_login that means the user is connected : const ROLE_ADMINISTRATOR = “administrator”; const ROLE_CONTRIBUTOR = “contributor”; add_action(“wp_login”, function ($user_login, \WP_User $user) { if ( !is_multisite() || is_user_member_of_blog() ) { return; } $usersAdministrator = get_users([ “role” => ROLE_ADMINISTRATOR, ]); … Read more

Hide from specific role the top admin bar new button

The item you want to remove is a “node”. When you look at the generated HTML of the WP toolbar, the ID of the list item (<li>) contains the node. So in this case, for the “Add New” toolbar item, you’ll see the following in the HTML: <li id=”wp-admin-bar-new-content” class=”menupop”> Everything after “wp-admin-bar-” in the … Read more

Programmatically allow a non-author to edit a post based custom meta field

Looking at current_user_can()‘s documentation, I see that it uses WP_User::has_cap(). So if your code (or the WP core code) uses something like current_user_can( ‘edit_post’, $post->ID ) to determine if the current user can edit the current post, you can use the user_has_cap filter (called in WP_User::has_cap()): add_filter( ‘user_has_cap’, ‘wpse360937_allow_manager’, 10, 4 ); function wpse360937_allow_manager( $allcaps, … Read more

Why is my Custom Post Type not showing up after adding capabilities?

You have to register custom capabilities and then apply inside the register post type function function register_website_caps() { $admins = get_role( ‘administrator’ ); $admins->add_cap( ‘edit_website’ ); $admins->add_cap( ‘read_website’ ); $admins->add_cap( ‘delete_website’ ); $admins->add_cap( ‘edit_websites’ ); $admins->add_cap( ‘edit_others_websites’ ); $admins->add_cap( ‘publish_websites’ ); $admins->add_cap( ‘read_private_websites’ ); } add_action( ‘admin_init’, ‘register_website_caps’); // Creating the website post type function … Read more

Enable specific CSS Code for Visitors and specific Roles

You can add custom css on your html header using wp_head hook action & checking your current User Role add_action(‘wp_head’, ‘add_css_of_current_specific_user_role’); function add_css_of_current_specific_user_role() { /*Check Current User is Login*/ if ( is_user_logged_in() ) { $user = wp_get_current_user(); /*Check Current User Role*/ if (in_array(‘visitor’, $user->roles)) { ?> <style> // Some CSS </style> <?php } } }

Filter list of rules based on a capability

Untested, but should be easily extendable(or something you can take ideas from). function roles_have_cap( $roles = false, $cap ) { global $wp_roles; if( !isset( $wp_roles ) || !isset( $cap ) ) return false; if( !$roles ) $roles = array_keys( $wp_roles->roles ); if( !is_array( $roles ) ) $roles = array( $roles ); $hascap = array(); foreach( … Read more

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