The problem is that wp_get_current_user (which current_user_can relies upon) is pluggable, which means it isn’t loaded until after plugins are loaded (to give plugins a chance to override it). That means it’s not available to call from the top level of a plugin file.
Instead, I’d make the role check inside the hook e.g.
function add_media_link_to_admin_menu( $wp_admin_bar ) {
if ( current_user_can( 'upload_files' ) ) {
// add Media Library to admin menu
$wp_admin_bar->add_menu(array(
'parent' => 'appearance',
'id' => 'media-library',
'title' => 'Media Library',
'href' => '/wp-admin/upload.php',
));
}
}
add_action( 'admin_bar_menu', 'add_media_link_to_admin_menu', 999 );
(However themes are loaded after pluggable functions, so your original code would work fine in a theme.)
Related Posts:
- Editor can create any new user except administrator
- Remove Ability for Other Users to View Administrator in User List?
- Groups of capabilities: users with multiple roles?
- User-edit role setting distinct from wp_capabilities? [closed]
- What’s the difference between the capability remove_users and delete_users?
- How to let contributors to create a new revision(draft) editing their published posts
- Disallowing Users of a Custom Role from Deleting or Adding Administrators?
- How to update user role without logout
- How to check user role without using current_user_can()
- Do not allow users to create new posts and pages
- \WP_User Object | What’s the Difference Between {caps} and {allcaps}?
- How to enable the theme editor cap for an editor role?
- How to assign capabilities to user NOT to User Role
- How-to Delay The Capability To Publish Posts?
- Is there a simple way to manage capabilities per user?
- Prevent Admin user to edit/see super admin from users list
- current user can edit user?
- Get User Role by ID not working
- Getting users by specific capability, not role
- Restricted registrations or removing the ability to edit your password/email
- Are User Levels Still Currently Used?
- Does an AJAX call on the Admin Side Automatically include the User Data/Capabilities?
- Remove Capabilities from WP admin for specific user role
- Add a role and give admin priviledges
- Where are $current_user->allcaps set?
- How can I prevent certain custom roles from seeing other custom roles on the user list page?
- What’s the correct way to add capabilites to user roles?
- Can I Create a Second Admin Level User Role?
- Custom capability for a single user
- Privilege to recover trashed posts
- Assigning multiple or additional capabilities to specific users or how to create additional roles like bbpress roles?
- how do I add role and capability after I create a new user
- How to make WordPress ‘editor’ role to list/view/add/edit users only with the role ‘author’?
- Author Error “Sorry, you are not allowed to access this page.”
- fine-grained capabilities for user related capabilities
- Disabling user capability to edit_posts or delete_posts in the front-end
- Groups roles & capabilities
- Subscriber role – blank page
- No one can edit post/pages by Administrator
- current_user_can() returning true for capability when the user and role do not have the capability
- WordPress auto login after registration not working
- Change the author slug from nickname to ID
- Retrieve all users from wordpress database via REST/JSON API
- Is there a way to set a user profile to Draft?
- Wp_User_Query not sorting by meta key
- pre_user_query meta_query admin user list
- Any reason to be concerned by a wave of “zombie” blog signups?
- Prevent user creating new users with specific roles
- Migrating users from .com to .org?
- Get total number of authors on the site
- Find out when a user was created and display varied content depending on time since creation
- Creating a Front-end based User Search
- Create user with author role but no login information
- How to add user meta for all users
- Add user data to table when user is created?
- Why wordpress showe “admin is editing…” whoever edits?
- Limit amout of sessions on user-by-user basis?
- Remove admin menu links for multiple users by email
- Use wp_update_user to update custom column in wp_users table
- Create a front user register/login/profile & logout without interfering with wp-login.php?
- C# user_nicename and Display name blank
- How to filter users by last_name?
- Custom Query for count_user_posts function
- How to get a users list by who created them?
- Does wordpress support natively the concept of logging-in users? (not admins, but users of the website)
- How to pull all the contributer users records and order by Designation (which is users meta data)?
- cannot get user_registered date from get_user_meta
- Display video on homepage for users who have not logged in
- Customising “user ids” and add to ‘user’ panel in the admin area
- Add a sub menu page to the Users menu
- Confirm Deletion not found
- One Click Access To Users Account In WordPress?
- How to use `wp_insert_user` & `wp_insert_post` simultaneously without `headers already sent` error?
- Need to exclude users with no posts from my contributors page
- Unexpected problems after importing WP data
- link variable to user
- WordPress user role with create user capability?
- How to add extra field in profile page and show in myaccount?
- Perform multiple actions after wp_insert_user()
- My custom user metadata is only active for only admin but i want it to be active on all users especially customers
- How to get image path from id using SQL
- How to allow access based on the user meta flag
- Authentication between two different sites using the WordPress login cookie
- How can I display Only the first Array/Object?
- How can i add user display name drop down menu in frontend?
- How to create page for user?
- Return all users that have one or more published blog posts
- Modify the user data stdObject and add extra items to it?
- wp_update_user() returning http 500 internal server error
- Change “logged in as a ” link in comments form
- Specific Content on pages based on user
- force registered user as seller
- Manage user profiles with WordPress
- Not able to call value in the core files
- Fetching posts that match a User Profile setting
- Will mass deleting WP Users then reimporting CSV with the same userid break WP?
- Privacy in WordPress
- how can i inform other users about new user registration? [closed]
- How to display Most Recently Read 10 Posts by a logged in user in wordpress
- Set a minimal number for next user_id