Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- var request = require("request");
- var options = { method: 'POST',
- url: 'https://hostname.com/wp-json/asdf/route/',
- headers:
- { 'Postman-Token': 'some-numbers',
- 'cache-control': 'no-cache',
- 'Content-Type': 'application/json' },
- body:
- { username: 'username',
- email: 'email@gmail.com',
- password: 'password' },
- json: true };
- request(options, function (error, response, body) {
- if (error) throw new Error(error);
- console.log(body);
- });
- var unirest = require("unirest");
- var req = unirest("POST", "https://hostname.com/wp-json/asdf/route/");
- req.headers({
- "Postman-Token": "some-numbers",
- "cache-control": "no-cache",
- "Content-Type": "application/json"
- });
- req.type("json");
- req.send({
- "username": "username",
- "email": "email@gmail.com",
- "password": "password"
- });
- req.end(function (res) {
- if (res.error) throw new Error(res.error);
- console.log(res.body);
- });
- add_action( 'rest_api_init', function () {
- register_rest_route( 'asdf', '/route/', array(
- 'methods' => 'POST',
- 'callback' => 'subscribe_user',
- ) );
- } );
- function subscribe_user( $data ) {
- $json_result = json_decode($data->get_body(), true); //note second param is for setting this to an associative array
- $username = $json_result["email"];
- $email = $json_result["email"];
- $password = $json_result["password"];
- $user_id = wp_create_user( $username, $password, $email );
- if ( is_wp_error( $user_id ) ) {
- return $user_id->get_error_message();
- }
- else {
- return 'User successfully registered!';
- }
- }
Add Comment
Please, Sign In to add comment