Wordpress Rest API with ReactJs | Custom User Field | GET & POST Example

Wesley Huber
2 min readOct 21, 2022

--

I had a lot of trouble finding a good example of this online, so I will show you guys really quickly how I was able to add a custom field to the Wordpress User Profile (in our case timezone):

And expose this field to the Wordpress Rest API GET & POST routes.

Here is my code for adding the field and exposing it to the Wordpress Rest API routes with callback functions for Get and Set:

The callback functions and the ‘show_in_rest’ => true are important for the Rest API to work with this field.

Here is the code I used in my React application to get and set the timezone field via the Wordpress API:

I just wanted to make this article to help somebody who might need it in the future to integrate something similar.

Creating this custom field and exposing the API routes definitely opened my eyes to the possibilities of using Wordpress as a “Headless CMS”. Essentially meaning — you can use the WP backend for admins & users and serve the frontend entirely in a React, Vue, Angular, or just a simple HTML/CSS/JS (Using Ajax) website / app.

You could even use react Native to integrate your CMS with a mobile App. Pretty cool.

Thanks for reading and peace out!

--

--