Support

Account

Home Forums Backend Issues (wp-admin) [Login WP] Connect single field to an external api

Solved

[Login WP] Connect single field to an external api

  • Hello everyone,

    I made a plugin to allow wordpress login with external api.

    Everything works, now what I have to do is that when a user logs in for the first time, the plugin checks to see if it is already present on wp, and where it was not already present, it creates a new user by taking behind username, email and password.

    The new user is created but I would like it to bring with it also the id field from the external api saving it in an ACF field.

    This is the code created so far:

    function au_auth($user, $username, $password)
    {
        $options = get_option('au_options');
        $endpoint = $options['au_apiurl'];
    
        $user_email_key = 'email';
        $password_key = 'password';
    
        // Makes sure there is an endpoint set as well as username and password
        if (!$endpoint || $user !== null || (empty($username) && empty($password))) {
            return false;
        }
    
        // Check user exists locally
        $user_exists = wp_authenticate_username_password(null, $username, $password);
    
        if ($user_exists && $user_exists instanceof WP_User) {
            $user = new WP_User($user_exists);
            return $user;
        }
    
        // Build the POST request
        $login_data = array(
            $user_email_key => $username,
            $password_key => $password
        );
    
        $auth_args = array(
            'method' => 'POST',
            'headers' => array(
                'Content-type: application/x-www-form-urlencoded'
            ),
            'sslverify' => false,
            'body' => $login_data
        );
    
        $response = wp_remote_post($endpoint, $auth_args);
    
        // Token if success; Not used right now
        $response_token = json_decode($response['response']['token'], true);
    
        $response_code = $response['response']['code'];
        if ($response_code == 400) {
            // User does not exist, send back an error message
            $user = new WP_Error('denied', __("<strong>Error</strong>: Your username or password are incorrect."));
        } else if ($response_code == 200) {
            // External user exists, try to load the user info from the WordPress user table
            $userobj = new WP_User();
            // Does not return a WP_User object but a raw user object
            $user = $userobj->get_data_by('email', $username);
            if ($user && $user->ID) {
                // Attempt to load the user with that ID
                $user = new WP_User($user->ID);
            }
        } else {
            // The user does not currently exist in the WordPress user table.
            // Setup the minimum required user information
            $userdata = array(
                'user_email' => $username,
                'user_login' => $username,
                'user_pass' => $password
            );
            // A new user has been created
            $new_user_id = wp_insert_user($userdata);
            // Assign editor role to the new user (so he can access protected articles)
            wp_update_user(
                array(
                    'ID' => $new_user_id,
                    'role' => 'editor'
                )
            );
            // Load the new user info
            $user = new WP_User ($new_user_id);
        }
    }
    // Useful for times when the external service is offline
    remove_action('authenticate', 'wp_authenticate_username_password', 20);
    
    return $user;
    }

    Anyone have any way how to help me?

  • After you insert the new user you need to get the value you want from the response and update the acf field.

    You need to use the Field Key because the value will not already exist

    
    $new_user_id = wp_insert_user($userdata);
    // get value from response you want to set
    update_field('field_XXXXXXXX', $value, 'user_'.$new_user_id);
    

    https://www.advancedcustomfields.com/resources/update_field/

  • I entered the string here, is that correct?

    
    // A new user has been created
                $new_user_id = wp_insert_user($userdata);
                // get value from response you want to set
                update_field('field_60084ad3970a8', $value, 'user_'.$new_user_id);
                // Assign editor role to the new user (so he can access protected articles)
                wp_update_user(
                    array(
                        'role' => 'editor'
                    )
                );
                // Load the new user info
                $user = new WP_User ($new_user_id);
    

    I did a test but I don’t populate the field, I attach image as an example (this user externally has an id 970)

    User ID (back WP)

  • you need to populate $value with the value from the json response or in some way get this value. I can’t help you there.

  • I apologize but I only now have a way to post my solution, maybe it can be useful to someone.

    In the meantime, thank you John for the support.

    Here is my solution to the problem:

    $new_user_id = wp_insert_user($userdata);
    $value = json_decode($response['body'], true)['Id'];
    // get value from response you want to set
    update_field('field_60084ad3970a8', $value0, 'user_'.$new_user_id);
Viewing 5 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic.