I found a better workaround using the same filter suggested by @ben-casey in his answer.
Firstly I registered throuhg register_rest_field
all the custom fields of all the user types I have in the database.
Then in rest_prepare_user
, insted adding the wanted fields, I removed the unwanted ones. In this way I can still use the same endpoint to update the field.
Below an example of the JSON clean up.
function my_rest_prepare_user( WP_REST_Response $response, WP_User $user, WP_REST_Request $request ){
if( in_array( 'administrator', $user->roles ) ){
$data = $response->get_data();
unset($data['custom_field']);
$response->set_data( $data );
}
return $response;
}
add_filter( 'rest_prepare_user', 'my_rest_prepare_user', 10, 3 );
Related Posts:
- How to show a gloabl message on a user profile page (in back end)?
- file upload user profile
- How to get custom post meta using REST API
- Is there a way to get protected meta fields through any of the available built-in WordPress APIs? (xmlrpc, wp-json)
- How to return Meta data from the REST API?
- Is it safe to store a user setting you don’t want the user to ever modify as a user option?
- How to get users by a custom field / by user meta data?
- How do you add a custom option to user data?
- Custom user avatar in the WordPress users listing
- User Profile / Add Custom Fields
- Retrieve custom fields on Categories, using WP-API
- How to filter or search the posts using postmeta tables custom meta fields with wordpress REST API
- Show User Their Password
- How to reload the role specific registration form on validation errors?
- Adding Properties to User Profile and Displaying in List
- Cannot edit post meta fields with rest API
- How can I sort get_users() by any value (last_name, user defined fields and more)
- Extra filed under “About the user” user profile
- get_user_meta Short Profile Section
- Transition from (classical) serialized custom meta field to (gutenberg) rest enabled meta
- Saving custom profile fields
- Redirect users based on custom field
- woocommerce product custom field
- Custom User Profile Fields
- Add More Fields to Users Pages (Admin Panel)
- WordPress User Profile Upload – If page is saved file reset
- Add conditional custom option to ‘Display name publicly as’ dropdown
- WP_User_Query pulling ACF to loop
- Custom Fields – Taller editing area
- Front-end editing with custom fields?
- WordPress Rest API to call page data associate with custom meta
- Add post meta fields, when creating a post using WordPress’ REST API
- Modifying custom order item meta from admin order view
- Is there a way to identify a user in a custom REST API method? [duplicate]
- Passing args to WP_User_Query am I using meta correctly?
- UPDATE on SELECT results. A SQL query to swap Author with Meta Box value
- Customizing user data
- Add additional ‘description’ field for defined roles / role metadata?
- Custom Field to a Role?
- Linking Custom Fields to Database Records
- Show user info in a hovercard [closed]
- Display Custom Field for a Specific Role, but not for Admin
- Customizing WP user profile with custom fields
- Iterate through users and display users meta info at front-page
- How to disable Edit Post and Allow only Custom Field?
- ACF: How to get users with a ACF flexible content subfield with a specific value AND layout?
- check for duplicate user meta data before updating
- how to make a custom field readonly or disabled by user role?
- Custom Field Repeating When Using foreach
- Front end register with custom fields
- Front-End User Profile
- Get emails from users by custom field SQL
- HM CMB: Role Select
- Save custom field types with this function…?
- How to parse a huge list of users using a CRON function?
- User can’t search himself on rest api
- Adding data to User profile
- Delete user meta but only if found in array
- How to update only certain custom fields in a the cache of a page?
- How to add a post’s view count into the WordPress API response
- How to save multiple custom user profile fields using repeater JQuery
- Custom meta POST request fired twice when updating a post in Gutenberg
- checking liked user in wordpress rest api
- Hide custom fields by user’s role
- Link fields in User Admin list
- Extend WP_User_Query or WP_User to return all custom fields?
- Echo text if field under user_meta is empty with get_users()
- Semi-Private Comments + page comments -> is it possible?
- Make Custom Fields Public in JSON – API
- Custom Admin Menu Report for Specific User ID
- Custom post type needs to capability to create and edit by some specific user role
- update_post_meta
- update_post_meta not working well
- How can I change author of posts to the value of one of the custom field of the posts?
- multiple meta_query and orderby question
- How to verify which WordPress user requested the API in ASP .NET Core?
- User query – getting values for custom meta keys/fields
- Expire Date Condition not Working ACF WordPress
- get_field values for each post on home page using wp_add_inline_style
- Listing posts with a value in a custom field
- Add a custom field to my custom post type and then get posts which are before the given date in the custom field
- Clone wp-admin/users.php (Users Admin Page)
- Change word in woocommerce product category custom field
- get posts based on non-single metadata
- Problem with revisions only returning four results
- Search & column order by meta value in admin
- Need to use WordPress page as authentication for different service
- How to set up page fields with gutenberg instead of ACF
- Update some (not all) post titles with custom field values before running the Loop
- Exclude URL’s from sanitize_html_classes
- Missing Custom Fields
- Best way to insert a list of links into a post outside of the_content
- Allow a user or role to view drafts and previews, but not other admin privileges?
- Automatically Add Tags Taxonomy in Post from Custom field’s value
- Automatically Add Custom Fields Value as Tags
- How to get data from user meta custom fields created with acf-pro plugin?
- How To Read Read Custom Post Type Data in Headless CMS Mode
- No plugin populate user information in to form
- How to check if a user is in a specific role?
- how to turn a link field into a button or link text