Guest User

Untitled

a guest
Jan 13th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. var request = require("request");
  2.  
  3. var options = { method: 'POST',
  4. url: 'https://hostname.com/wp-json/asdf/route/',
  5. headers:
  6. { 'Postman-Token': 'some-numbers',
  7. 'cache-control': 'no-cache',
  8. 'Content-Type': 'application/json' },
  9. body:
  10. { username: 'username',
  11. email: 'email@gmail.com',
  12. password: 'password' },
  13. json: true };
  14.  
  15. request(options, function (error, response, body) {
  16. if (error) throw new Error(error);
  17.  
  18. console.log(body);
  19. });
  20.  
  21. var unirest = require("unirest");
  22.  
  23. var req = unirest("POST", "https://hostname.com/wp-json/asdf/route/");
  24.  
  25. req.headers({
  26. "Postman-Token": "some-numbers",
  27. "cache-control": "no-cache",
  28. "Content-Type": "application/json"
  29. });
  30.  
  31. req.type("json");
  32. req.send({
  33. "username": "username",
  34. "email": "email@gmail.com",
  35. "password": "password"
  36. });
  37.  
  38. req.end(function (res) {
  39. if (res.error) throw new Error(res.error);
  40.  
  41. console.log(res.body);
  42. });
  43.  
  44. add_action( 'rest_api_init', function () {
  45. register_rest_route( 'asdf', '/route/', array(
  46. 'methods' => 'POST',
  47. 'callback' => 'subscribe_user',
  48. ) );
  49. } );
  50.  
  51.  
  52. function subscribe_user( $data ) {
  53. $json_result = json_decode($data->get_body(), true); //note second param is for setting this to an associative array
  54. $username = $json_result["email"];
  55. $email = $json_result["email"];
  56. $password = $json_result["password"];
  57.  
  58. $user_id = wp_create_user( $username, $password, $email );
  59. if ( is_wp_error( $user_id ) ) {
  60. return $user_id->get_error_message();
  61. }
  62. else {
  63. return 'User successfully registered!';
  64. }
  65. }
Add Comment
Please, Sign In to add comment