multisite - Synchronizing User Accounts Between Main and Sub WordPress Sites

admin2025-01-08  4

I have two WordPress sites: xxx and deneme.xxx. I want to send the user data (username, password, email, first name, last name) of a user who registers on xxx to deneme.xxx via an API. I’ve written the following code that sends the data, but I am having an issue with sending the password. When the user tries to log in on the sub-site, they get an incorrect password error.

Code added to the main site (xxx):

add_action('user_register', 'send_user_data_to_subsite', 10, 1);

function send_user_data_to_subsite($user_id) {
    // Get new user data
    $user = get_userdata($user_id);
    $username = $user->user_login;
    $first_name = $user->first_name;
    $last_name = $user->last_name;
    $email = $user->user_email;
    $password = $user->user_pass;  // Get the actual password here

    // Send data to the API endpoint
    $response = wp_remote_post('/', [
        'method'    => 'POST',
        'body'      => json_encode([
            'username'  => $username,
            'first_name'=> $first_name,
            'last_name' => $last_name,
            'email'     => $email,
            'password'  => $password,  // Sending password as plain text
        ]),
        'headers'   => [
            'Content-Type' => 'application/json',
        ],
    ]);

    $body = wp_remote_retrieve_body($response);
    // Handle the response
}

Code added to the sub-site (deneme.xxx):

add_action('rest_api_init', function () {
    register_rest_route('my_namespace/v1', '/sync_user/', [
        'methods' => 'POST',
        'callback' => 'sync_user_from_another_site',
        'permission_callback' => '__return_true',
    ]);
});

function sync_user_from_another_site(WP_REST_Request $request) {
    $data = $request->get_json_params();

    // Get the data
    $username = sanitize_text_field($data['username']);
    $first_name = sanitize_text_field($data['first_name']);
    $last_name = sanitize_text_field($data['last_name']);
    $email = sanitize_email($data['email']);
    $password = sanitize_text_field($data['password']);  // Get password as plain text

    // Create new user (using the plain text password here)
    $user_id = wp_create_user($username, $password, $email);
    if (is_wp_error($user_id)) {
        return new WP_REST_Response(['success' => false, 'message' => 'User could not be created.'], 500);
    }

    // Update user info
    wp_update_user([
        'ID' => $user_id,
        'first_name' => $first_name,
        'last_name' => $last_name,
    ]);

    return new WP_REST_Response(['success' => true, 'message' => 'User successfully synchronized.'], 200);
}

Purpose of this code: xxx is my main site. Anyone who becomes a member here must automatically become a member of my sub-site trial.xxx. I want to enable the user to log in to the subsite with the data he used on the main site.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736267933a1233.html

最新回复(0)