• Hello,

    I am trying to add age field to register screen, and I added it to profile field in WP however I still cant post that information to the system from my app and I can show it on the app if I entered it manually to a user, any help please? I know the function the need to edit is public function register() in fluttter-user.php

    here is how it looks like after editing but still not working:

      public function register()
        {
            $json = file_get_contents('php://input');
            $params = json_decode($json, TRUE);
            $usernameReq = $params["username"];
            $emailReq = $params["email"];
            $role = $params["role"];
            if (isset($role)) {
                if (!in_array($role, ['subscriber', 'wcfm_vendor', 'seller', 'wcfm_delivery_boy', 'driver','owner'], true)) {
                    return parent::sendError("invalid_role", "Role is invalid.", 400);
                }
            }
            $userPassReq = $params["user_pass"];
            $userLoginReq = $params["user_login"];
            $userEmailReq = $params["user_email"];
            $userAge = $params["age"];
    
            $username = sanitize_user($usernameReq);
    
            $email = sanitize_email($emailReq);
            if (isset($params["seconds"])) {
                $seconds = (int)$params["seconds"];
            } else {
                $seconds = 1209600;
            }
    
            if (!validate_username($username)) {
                return parent::sendError("invalid_username", "Username is invalid.", 400);
            } elseif (username_exists($username)) {
                return parent::sendError("existed_username", "Username already exists.", 400);
            } else {
                if (!is_email($email)) {
                    return parent::sendError("invalid_email", "E-mail address is invalid.", 400);
                } elseif (email_exists($email)) {
                    return parent::sendError("existed_email", "E-mail address is already in use.", 400);
                } else {
                    if (!$userPassReq) {
                        $params->user_pass = wp_generate_password();
                    }
    
                    $allowed_params = array('user_login', 'user_email', 'user_pass', 'display_name', 'user_nicename', 'user_url', 'nickname', 'first_name',
                        'last_name', 'description', 'rich_editing', 'user_registered', 'role', 'jabber', 'aim', 'yim',
                        'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'age',
                    );
    
                    $dataRequest = $params;
    
                    foreach ($dataRequest as $field => $value) {
                        if (in_array($field, $allowed_params)) {
                            $user[$field] = trim(sanitize_text_field($value));
                        }
                    }
    
                    $user['role'] = isset($params["role"]) ? sanitize_text_field($params["role"]) : get_option('default_role');
                    $user_id = wp_insert_user($user);
    
                    if (is_wp_error($user_id)) {
                        return parent::sendError($user_id->get_error_code(), $user_id->get_error_message(), 400);
                    } elseif (isset($params["phone"])) {
                        update_user_meta($user_id, 'billing_phone', $params["phone"]);
                        update_user_meta($user_id, 'registered_phone_number', $params["phone"]);
                         update_user_meta( $user_id, 'age', $_POST['age'] );
                        
                        update_user_meta($user_id, 'age', $allowed_params["age"]);
                        
                    }
                 
                }
            }
            wp_new_user_notification($user_id, null, 'both');
            $cookie = generateCookieByUserId($user_id,  $seconds);
    
            return array(
                "cookie" => $cookie,
                "user_id" => $user_id,
            );
        }
  • The topic ‘Add more field to user register form’ is closed to new replies.