5

I formerly had the JSON API and JSON USER API plugins working before 4.7 and I have seen the documentation at the ReST API User reference, https://developer.wordpress.org/rest-api/reference/users/#create-a-user%20%22ReST%20API%20reference but I don't know how to get started. I'm sure there is an authentication procedure that must happen first to get a nonce, but I don't know how to do that anymore either. I'd really appreciate it if someone would show me or point me to some example cURL statements and URI's for both getting the nonce and creating a user.

TIA

2 Answers 2

7

The REST API included in WordPress doesn't actually have authentication built into it.

If you do normal authentication in WordPress by logging in, then your browser will receive a set of cookies. If you send those cookies along with your request, then that will authenticate you to perform the actions in question.

If you need to add authentication for an external service, then you need to install a plugin to handle it. A few are available:

If you're just testing locally, there is also a Basic Authentication plugin which allows you to simply send your username and password with every request in an Authorization header:

In any case, once you've either gotten the proper cookie or enabled the authentication method, then creating a new user is simple. Send a POST request to /wp-json/wp/v2/users with the username, email, and password as the payload.

You can find this documented here:

https://developer.wordpress.org/rest-api/using-the-rest-api/

https://developer.wordpress.org/rest-api/using-the-rest-api/authentication/

4

In order to ‘Register User’ or ‘Retrieve Password’, the authentication for a user with ‘Administrator’ role is required. While this is a delibrately done for security reasons, such implementation makes it very hard for Front End applications to implement a simple ‘Register’ or ‘Sign Up’ function.

The simple solution is use this wp-rest-user plugin. This plugin full fill such requirement by extending the existing WordPress REST API endpoints.

USAGE

Send a POST request to /wp-json/wp/v2/users/register, including a JSON body with three keys: username, email and password.

Send a POST request to /wp-json/wp/v2/users/lostpassword, including a JSON body with three keys: user_login.

You can use postman to as bellow picture enter image description here

Or even ajax request, please see the code bellow

<script>
    function addUserData(){
            $.ajax( {
            url: 'http://localhost/lapaktrip/wp-json/wp/v2/users/register',
            method: 'POST', 
            contentType: "application/json; charset=utf-8",  
            dataType: "json", 
            data:JSON.stringify({
                'username' : 'testmember',
                'email' : '[email protected]',
                'password' : '123456'
            })
        } ).done( function ( response ) {
            console.log( response );
        } )
    }           
</script>
<button onclick="addUserData()">Start Sending</button>

Hope this answer can help many people out there. Happy Codding.

1
  • This plugin has been closed as of July 13, 2021 and is not available for download. Reason: Guideline Violation.
    – Seano
    Jun 18 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.