• Skip to primary navigation
  • Skip to main content
OsomCode

OsomCode

Awesome code tutorials for WordPress and Genesis

  • Subscribe
  • My account
    • Edit profile
    • Favorites
    • Update credit card
  • Login

Create an identical user on two WordPress installations using the REST API

Discover in this tutorial how to create an identical user in another WordPress installation using the REST API when a user registers on a website.

In this tutorial you are going to learn how you can use the REST API so that when a user registers on web1, an identical user is created on web2.

This can be very interesting if you want the users of one website to have access to another as part of a subscription or membership.

Let’s see how to achieve this:

Instructions for registering an identical user on another WordPress installation using the REST API

1. Create a password for the REST API

The first thing you need to do is to create a new application password in the WordPress installation where you want the same user (web2) to be created as in the original entry (web1).

To do this, on web2, go to Users > Profile and scroll down to the “Application Passwords” section.

Then fill in the new password name field (it can be anything) and click on “Add a new application password”.

setting-create-password-application-API-REST-WordPress

Once done, the password name and password will be displayed. Be sure to copy it and keep it safe because it will not be displayed again.

password-API-REST-WordPress

2. Sends the user and their details to another WordPress using the REST API

To get the information about the users you are going to use the REST API endpoint that gives you access to that information.

Add the following code at the end of your web1 functions.php or functionality plugin:

To see this and another 847 code snippets of this website, login or subscribe here.

With this code you use the user_register hook to run when there is a new registration and the user (and all the data you decide) is sent to the other WordPress installation.

In the first part of the snippet you define the variables:

  • $api_username = WordPress username
  • $api_password = Application password obtained in the previous step
  • $website_url = The URL of web2
  • $request_url= The URL where the request is made. In this case it is the concatenation: $website_url . '/wp-json/wp/v2/v2/users' which accesses the endpoint for REST API entries.

Remember to change the value of the variables to those of your case.

Then you use the wp_remote_request function to get the endpoint for users and using the POST method you pass the user data. In this case:

  • 'username' = Username
  • 'email' = Email
  • 'password' = Password
  • 'first_name' = First name
  • 'last_name' = Last_name
  • 'url' = URL of the website
  • 'roles' = user roles

But you can add more to customize it to your liking. These are all the details you can send using the POST method to the user endpoint:

  • username
  • name
  • first_name
  • last_name
  • email
  • url
  • description
  • locale
  • slug
  • roles
  • password
  • meta

You have more information about it on official WordPress developer page.

If you have any questions or suggestions you can leave them in the comments.

Conclusions

Now you know how to use the REST API to create a user in another WordPress installation.

Any questions? I read you in the comments.

And if you want to give me any suggestion for future tutorials, leave it in the contact form. Advantages of being a subscriber. 😉

PHP REST API WordPress

To leave read and make questions about this code, you can login or register.

  • About OsomCode
  • FAQ
  • Contact

Legal Notice · Privacy Policy · Cookie Policy · Terms and conditions
Copyright © 2023

Lost your password?