First, you can get the current user object like so: $user = wp_get_current_user();
.
The $user
object would have a roles
property containing the user’s roles, so you can do the following to check if the current user has a specific role: in_array( 'role_slug', $user->roles )
.
And then in your field HTML, you can use the disabled()
and readonly()
functions to easily add the disabled
and readonly
attributes:
<?php
// Get the current user object.
$user = wp_get_current_user();
// Check if the associated user has a specific role.
$has_role = in_array( 'administrator', $user->roles );
?>
<input name="some_name" <?php disabled( true, ! $has_role ); ?> />
<input name="some_name2" <?php readonly( true, ! $has_role ); ?> />
And the above would make the field be disabled/readonly if the current user doesn’t have the role administrator
(note the ! $has_role
).
Related Posts:
- Modify custom Users Manage page
- Creating user relationships
- No plugin populate user information in to form
- Change the functionality of wordpress users module in admin
- How to update the delete user confirmation form?
- Capability to edit own posts and not others
- How to remove Profile Picture section or the message “You can change your profile picture on Gravatar.”
- How to Save Different Usermeta Fields According to User Role?
- Add field to posts (checkbox) that only one post can have
- generate unique number when registering a user
- How do I properly format the user_role array?
- How to get value from custom backend menu
- Adding Properties to User Profile and Displaying in List
- Allow Users to Set Password During Registration
- Custom protected password page with ACF
- Set user loggin status?
- Community and User Generated Content Website: Have been thinking about Drupal but leaning on WordPress heavily
- How can I add data to a custom column in the Users section of the wordpress backend?
- Passing page url into a custom field via the Press-This bookmarklet
- How to extend nav-menu-item-control data in Appearance > Customize?
- Custom column sorting in WordPress admin post table
- Last time a user logged in
- User Roles Not Sticking
- Hide custom field if empty
- Updating Custom WordPress User Meta Field via REST API
- How to pass external variables to the wp_new_user_notification_email filter?
- How can I allow access to multiple users, using the same login, at the same time?
- Register rest field for specific user
- Query posts by category AND custom field, then ORDERBY another custom field – help!
- list/consult Taxonomy only for the respective author/creator in dashbboard
- Function to allow “Anyone can register”?
- Make custom user table column filterable
- Advanced Custom Fields: Sorting custom columns with custom fields sorts only by date
- How to show a gloabl message on a user profile page (in back end)?
- Add ‘Creator’ User Meta when adding user
- Follow me for WordPress blogs (multi-authors)
- I want to submit multiple users if checkbox is checked
- How to change popular term checklist title when edit post?
- Add a date updated/date modified option using custom fields
- Change the Labels of Users to Customers in Admin
- Woocomerce custom billing address fields display in order details [closed]
- User can not login
- Allow guests to use WP Customizer but disable save button
- Users can only view their content from the front end
- Custom Role with Own Page Edit Capability
- List top 5 authors with most posts
- What requirements for ACF custom fields key and name properties?
- Multi-site User Sessions
- Problem with custom user meta field
- How to insert a writing panel between the title and the page content?
- Toggle custom fields?
- Customizing Users in Admin Area
- How to stop displaying the Id without losing the functionality
- Restrict access to non-wordpress section of site with user roles?
- How to print Array values of custom Fields Meta?
- Get users based on month ACF datepicker field
- What is the best way to modify Admin Panel on WordPress?
- How to make wordpress use a non-wordpress users table?
- Allow multisite admins only to create users with one specific user role?
- Hide content and functions from specific user roles
- Let author add field to metabox by pressing a button
- How to auto add nofollow to links in custom field?
- Save custom field types with this function…?
- Personalized Gallery what are my options?
- how or where wordress collect unlogged user session
- Is it possible to add custom badges to product attributes conditionally, based on custom field?
- WordPress category custom title
- Simple custom attributes on posts
- Use a custom page with dynamic content as a user profile page
- Query Product categories what have a custom field
- Invitation link to a site in multisite network does not work?
- Adding a drop-down (select tag) option under Menu Settings in WordPress Admin
- WP_User_Query Search Multiple Keywords from a Multi-Select Field?
- Custom roles and capabilities media not work right
- Replace publish date in blog module with custom field
- Show comments based on user role in front end
- How do I enable the text editer in my WordPress theme? [closed]
- How to display all post from the same current usermeta
- How to add H1 Tag on Homepage?
- Add commentmeta custom field to every published comment with SQL
- Highlight specific user with custom field value
- Does WordPress support template resources/custom fields
- Disallow “contributors” to edit their own posts once they’ve been set to “Pending Review”
- Changing starting number of User ID
- Custom search by several options send on form not works
- User registration add user ID?
- What capability is required to view posts, scheduled in the future, by someone else?
- How to create functions that affect only a specific user
- Semi-Private Comments + page comments -> is it possible?
- Security Issues with custom user role or changing core role capabilities?
- Create users via PHP
- Use wp_login_form function to login with a custom user table?
- file upload user profile
- How to give different user access to different people?
- Save And Display Mp3 Audio files on WooCommerce product page with Dokan
- Advice how to connect custom database with WordPress, change data in WordPress and finaly use this data
- Is it possible assign a secondary existing role to already existing one?
- User Query Multiple Orderby Clause
- Custom Admin Menu Report for Specific User ID
- Add custom information in editor summary