This is How to Update Custom User Meta Through the WordPress REST API
As a WordPress developer, you may often need to work with custom user meta, which are additional data fields associated with user accounts. While the WordPress REST API provides a handy way to interact with user data, updating custom user meta can sometimes be tricky.
In this article, we'll dive into the problem and provide a solution to help you successfully update custom user meta through the WordPress REST API.
The Problem: Updating Custom User Meta Through the REST API
The standard WordPress REST API endpoints, such as the /wp/v2/users
endpoint, allow you to retrieve and update user data. However, when it comes to custom user meta, things can get a bit more complicated.
By default, the REST API does not automatically include or allow updating of custom user meta fields. To access and update these fields, you need to register them explicitly using the register_rest_field()
function.
Here's an example of how you might try to update a custom user meta field called "bookmarks" through the REST API:
function handle_user_bookmarks() {
register_rest_field( 'user', 'bookmarks', array(
'get_callback' => array( $this, 'get_user_bookmarks' ),
'update_callback' => array( $this, 'add_user_bookmarks' ),
'schema' => null
));
}
function get_user_bookmarks( $user, $field_name, $request ) {
return get_user_meta( $user['id'], $field_name, true );
}
function add_user_bookmarks( $user, $meta_value ) {
$bookmarks = get_user_meta( $user['id'], 'bookmarks', false );
if( $bookmarks ) {
update_user_meta( $user['id'], 'bookmarks', $meta_value );
} else {
add_user_meta( $user['id'], 'bookmarks', $meta_value, true );
}
}
This code sets up the necessary hooks to register the "bookmarks" custom user meta field with the REST API. The get_user_bookmarks()
function retrieves the current value of the "bookmarks" field, while the add_user_bookmarks()
function updates or creates the custom user meta.
However, as the description mentions, the original code had an issue with the $user['id']
parameter. This is because the $user
parameter passed to the add_user_bookmarks()
function is not an array, but rather a WP_User object.
The Solution: Updating Custom User Meta with the Correct User ID
To fix the issue, you need to use the correct property to access the user ID. Instead of $user['id']
, you should use $user->ID
.
Here's the corrected code:
function handle_user_bookmarks() {
register_rest_field( 'user', 'bookmarks', array(
'get_callback' => array( $this, 'get_user_bookmarks' ),
'update_callback' => array( $this, 'add_user_bookmarks' ),
'schema' => null
));
}
function get_user_bookmarks( $user, $field_name, $request ) {
return get_user_meta( $user['id'], $field_name, true );
}
function add_user_bookmarks( $user, $meta_value ) {
$bookmarks = get_user_meta( $user->ID, 'bookmarks', false );
if( $bookmarks ) {
update_user_meta( $user->ID, 'bookmarks', $meta_value );
} else {
add_user_meta( $user->ID, 'bookmarks', $meta_value, true );
}
}
The only change is in the add_user_bookmarks()
function, where we replaced $user['id']
with $user->ID
. This ensures that the correct user ID is used when updating the custom user meta.
With this correction, the code should now work as expected, allowing you to update the "bookmarks" custom user meta field through the WordPress REST API.
Understanding the Code
Let's break down the code and understand how it works:
-
handle_user_bookmarks()
: This function is responsible for registering the custom user meta field "bookmarks" with the REST API. It uses the register_rest_field()
function, which takes the following parameters:
'user'
: The post type to which the field should be added (in this case, the "user" post type).
'bookmarks'
: The name of the custom user meta field.
'get_callback'
: A function that retrieves the value of the custom user meta field.
'update_callback'
: A function that updates the value of the custom user meta field.
'schema'
: The schema definition for the custom user meta field (optional).
-
get_user_bookmarks()
: This function is the "get_callback" for the custom user meta field. It retrieves the value of the "bookmarks" field using the get_user_meta()
function, passing the user ID ($user['id']
) and the field name ($field_name
).
-
add_user_bookmarks()
: This function is the "update_callback" for the custom user meta field. It first checks if the "bookmarks" field already exists for the user using get_user_meta()
. If the field exists, it updates the value using update_user_meta()
. If the field does not exist, it creates a new one using add_user_meta()
.
The key to the solution is understanding that the $user
parameter passed to the add_user_bookmarks()
function is a WP_User
object, not an array. Therefore, you need to use the $user->ID
property to access the correct user ID, instead of $user['id']
.
Conclusion
Updating custom user meta fields through the WordPress REST API can be tricky, but with the right approach, you can make it work. By registering the custom user meta field and using the correct user ID, you can successfully update the custom user meta data.
If you're working on a project that involves custom user meta and the WordPress REST API, remember this solution to help you overcome any similar issues. And if you're looking for a powerful tool to analyze your website's user behavior and generate conversion-boosting recommendations, be sure to check out Flowpoint.ai.
Get a Free AI Website Audit
Automatically identify UX and content issues affecting your conversion rates with Flowpoint's comprehensive AI-driven website audit.