iOS Swift / Wordpress API - POST to custom fields user registration?

Viewed 33

When users register on my app, a user is created inside my Wordpress installation as well. I'm using Alamofire and the Wordpress API to accomplish this. That said, my user is created successfully, but for some reason any custom fields I have on the registration form aren't filled by my below code. The only fields that are filled properly are username, email and password.

How can I make it so that I'm able to POST data to custom fields on the registration form? Oddly, first_name & last_name are Wordpress fields (not custom) and data isn't saved to those either despite me POSTing to them.

My code below:

ViewController

// CREATE THE USER ACCOUNT IN WORDPRESS
        
      @IBAction func registerNow(_ sender: Any) {
                 
                let params: Parameters = [
                    "username": emailAddress.text!,
                    "password": passwordField.text!,
                    "email": emailAddress.text!,
                    "nickname": firstName.text!,
                    "last_name": lastName.text!,
                    "first_name": firstName.text!,
                    "phone_number": phoneNumber.text!
                    
                    ]
                
        
                AF.request("http://myurl.com/wp-json/wp/v2/users/register", method: .post, parameters: params, encoding: JSONEncoding.default, headers: nil).validate(statusCode: 200 ..< 299).responseJSON { AFdata in
                    do {
                        guard let jsonObject = try JSONSerialization.jsonObject(with: AFdata.data!) as? [String: Any] else {
                            print("Error: Cannot convert data to JSON object")
                            return
                        }
                        guard let prettyJsonData = try? JSONSerialization.data(withJSONObject: jsonObject, options: .prettyPrinted) else {
                            print("Error: Cannot convert JSON object to Pretty JSON data")
                            return
                        }
                        guard let prettyPrintedJson = String(data: prettyJsonData, encoding: .utf8) else {
                            print("Error: Could print JSON in String")
                            return
                        }
                        
                        print(prettyPrintedJson)
                    } catch {
                        print("Error: Trying to convert JSON data to string")
                        return
                    }
                }
                
    }
1 Answers

The WordPress Core REST API does not include a user registration endpoint.

Endpoint list for Users: https://developer.wordpress.org/rest-api/reference/users/

There is a Create a User (POST /wp/v2/users) endpoint, but only logged-in admin users can add users on that endpoint if they have the create_users capability.


So the endpoint you specified is your own code. Thus, the error must be looked for primarily in that PHP code. Your related question and the answer: WordPress Rest API - Register user - fields not saving?

Related