最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

How do I create a user using the new JSON api in 4.7?

programmeradmin2浏览0评论

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, 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

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/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

Share Improve this question asked Apr 21, 2017 at 19:59 n8barn8bar 2191 gold badge3 silver badges11 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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:

  • https://wordpress/plugins/rest-api-oauth1/
  • https://wordpress/plugins/application-passwords/
  • https://wordpress/plugins/jwt-authentication-for-wp-rest-api/

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:

  • https://github/WP-API/Basic-Auth

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/rest-api/using-the-rest-api/

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

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

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.

发布评论

评论列表(0)

  1. 暂无评论