• Hello, do you know if it is possible to register the first name and the last name?

    {
    “username”: “naviprueba1718”,
    “email”: “[email protected]”,
    “password”: “navi1718”,
    “first_name”: “lala”,
    “last_name”: “pepe1”

    }

    it registers but leaves the name and surname blank

Viewing 2 replies - 1 through 2 (of 2 total)
  • By default, it is not possible to save extra fields other than username/email/password, but you can achieve this with the wp_rest_user_user_register hook:

    
    /** Custom extra fields on user registration. */
    add_filter( 'rest_pre_dispatch', 'rest_store_request', 10, 3 );
    add_action( 'wp_rest_user_user_register', 'save_custom_user_fields' );
    
    /** Save HTTP parameters in a global var, to be used later on "save_custom_user_fields". */
    function rest_store_request( $result, $server, $request ) {
      global $rest_request;
      $rest_request = $request;
      return $result;
    }
    
    /** Called on register user - add extra custom fields. */
    function save_custom_user_fields( $user ) {
      /** Get inputs from POST. */
      global $rest_request;
      $parameters = $rest_request->get_json_params();
    
      /** Sanitization. */
      $first_name = sanitize_text_field( $parameters['first_name'] );
      $last_name  = sanitize_text_field( $parameters['last_name'] );
    
      /** Add meta records. */
      add_user_meta( $user->ID, 'first_name', $first_name, true );
      add_user_meta( $user->ID, 'last_name', $last_name, true );
    
      /** Update user record. */
      $user->first_name = $first_name;
      $user->last_name  = $last_name;
      wp_update_user( $user );
    
      /** "New user registration" email to admin: uncomment to enable it. */
      // wp_send_new_user_notifications( $user->ID, 'user' );
    }
    

    You can paste this code to your functions.php.

    Any ideas?

    I’m using this plugin with Daniel’s solution to allow creation of the user adding more fields than just username, email and password.

    I’m getting this error.

    {
    “code”: “rest_cannot_edit”,
    “message”: “Sorry, you are not allowed to edit this user.”,
    “data”: {
    “status”: 401
    }
    }

    Thanks for any help you might give!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Hello, do you know if it is possible to register the first name and the lastname’ is closed to new replies.