Reducing the number of checks increases the performance of your code, so yes, check is_user_logged_in()
and current_user_can()
as few times as you can.
For executing functions depending on the admin page, I’d probably attach callbacks to load-{$pagenow}
hook (untested):
function wpse417218_do_something_for_edit_page() {
if ( 'page' !== get_current_screen()->id ) {
return;
}
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
// Do something.
}
add_action( 'load-post.php', 'wpse417218_do_something_for_edit_page' );
This will keep everything organized, and also check the user’s capabilities only if on the appropriate page.
Related Posts:
- allow editors to edit menus?
- How to change a user’s role?
- Is there way to rename user role name without plugin?
- How to add a Capability to a User Role?
- How can I get a list of users by their role?
- Reset default roles and capabilities
- Is it possible to add new user Roles?
- Temporarily give ‘manage_options’ capability
- Hide specific admin users’ posts
- How to filter the role selector?
- How can I change a user role upon visiting a page?
- How to let the Contributor role preview unpublished posts?
- Assign Subscriber capabilities to a custom user role
- Roles at registration (classipress)
- How to allow registered users to change their user role through frontend?
- how to change user roles for users who doesn’t have any. (about 8000 users)
- Shold I manually add ‘cap’ to admin role ?
- Contributors missing from author dropdown
- Getting a user role from the user login name
- How to reload the role specific registration form on validation errors?
- Why is wp-login redirecting to the home page when I use this function?
- Allow contributor to view own scheduled post
- Editor and contributor roles not correct after adding function
- How to add author role as a custom field to custom post type in wordpress?
- How are roles stored in the database?
- How to delete user roles?
- How to display user role
- I am adding a new class to my body tag if the logged in user is subscriber, need help
- How to Orderby User Role?
- Users Role and Access
- Prevent custom user role to add new pages
- Hide Specific User Page
- The delete_posts capability?
- Capabilites not working [closed]
- Why does this check to see if user is authorized to edit a post fail for all but super admins?
- get approved users only ( ultimate member plugin )
- How to allow a user to make their post (ad) a draft, and then publish again without needing approval?
- Create Super Admin that cant be deleted
- Assign second role to user
- Adding Capabilities to a WordPress User Account
- Adding an additional role to an Administrator
- Allow editors to post iframes [duplicate]
- if user has a certain role then display image
- Parent User and Child User – relate users
- My subscriber has the “edit comment” capability but can’t edit comment
- New user is assigned 2 roles: customer and superadmin
- bbPress plugin moderator roles
- how to set any post creation for pending status and let the admin approve it first?
- Allowing user to edit posts based on the post status
- Add custom role across network in multisite
- Allow user role “salesman” to view only “client” role users
- Purpose of Adding Capability to Role But Not Grant?
- bbPress: How to set conditional for specific user -or- user role, to be displayed in user profile
- Menu page with list of users
- How to check user role?
- how to remove some permissions from a shop “manager role” in woocommmerce?
- reset the users roles – is there a way to do this?
- Get and display a user’s profile info?
- How to get a users list by who created them?
- Paypal API and WordPress
- Is there a Capability that checks as true for non-logged in users?
- Custom wordpress admin page/url “You do not have sufficient permissions to access this page.”
- user_can() not working for comment authors
- How to write conditions based on user capabilities not on user role?
- WordPress add_rewrite_rule redirection match GET variable not passing through to custom template
- remove_cap nothing changes
- get_post_meta with related user
- How to change a user’s role depending on date registered?
- Migrating Roles from one environment to another
- Need to block user role from accessing bbPress all together
- Show special backend content for certain roles
- How to create a front facing user sign up, log in and profile pages like FoodGawker.Com [closed]
- Why can my subscribers create new posts for review?
- Custom Role read_private_posts Not Working
- Learndash change user role after completing the course
- How to disable activation email to specific user role?
- logout users with specific role after close browser tab
- Admin user name not showing up in author dropdown
- Separate user bases or hide users of another role or connected to another minisite
- How to restrict subscriber editing other posts but read specific posts in backend
- Saving user meta “member_id” based on user role
- Change logo url based on WP user role
- Display specific post categories to specific user roles
- Publishing post strips custom html element when user is not admin
- Role and Capabilities: How do I allow user role to access theme options without enabling ‘manage_options’?
- Best practices to handle multilpe roles and capabilities?
- Allow multiple roles to specific user
- user has permission to read a custom post but it is not being shown in wordpress plugin
- Restrict custom fields based on user roles
- Select dropdown with 2 choices from foreach
- Restrict certain roles registrations by domain
- translate_user_role doesn’t work
- Allow signed in users (subscribers) to see their drafts
- How to sync user database with “user role editor” plugin enabled
- Need to create admin user without capability to create user
- Give editor ability to edit custom settings without access to Main Settings
- How to assign role to a custom registration form?
- How to add user roles? [closed]
- Check if specific role exists
- How can I display customizer’s “Additional CSS” to administrators and editors (in multisite)?