add_action('rest_api_init', function () {
register_rest_route('wpsync/v1', '/add-user', [
'methods' => 'POST',
'callback' => function($request) {
global $wpdb;
$table = $wpdb->prefix . 'custom_users';
// Decode incoming JSON
$data = json_decode($request->get_body(), true);
// Minimal required fields
$email = sanitize_email($data['email'] ?? '');
$first = sanitize_text_field($data['first_name'] ?? '');
$last = sanitize_text_field($data['last_name'] ?? '');
$phone = sanitize_text_field($data['phone'] ?? '');
if (!$email || !$phone) {
return ['error' => 'Missing email or phone'];
}
// Create WP user
$username = sanitize_user(strtolower($first . '.' . $last));
$user_id = wp_create_user($username, wp_hash_password($phone), $email);
if (is_wp_error($user_id)) {
return ['error' => $user_id->get_error_message()];
}
// Insert into custom table
$wpdb->insert(
$table,
[
'wp_user_id' => $user_id,
'username' => $username,
'email' => $email,
'password_hash' => wp_hash_password($phone),
'first_name' => $first,
'last_name' => $last,
'display_name' => trim("$first $last"),
'created_at' => current_time('mysql'),
],
['%d','%s','%s','%s','%s','%s','%s','%s']
);
if ($wpdb->last_error) {
return ['db_error' => $wpdb->last_error];
}
return ['status' => 'created'];
},
'permission_callback' => '__return_true',
]);
});
Warning: Cannot modify header information - headers already sent by (output started at /home/triaug5/public_html/wp-content/plugins/WPSYNC_test/WPSYNC_test.php:1) in /home/triaug5/public_html/wp-includes/pluggable.php on line 1531
Warning: Cannot modify header information - headers already sent by (output started at /home/triaug5/public_html/wp-content/plugins/WPSYNC_test/WPSYNC_test.php:1) in /home/triaug5/public_html/wp-includes/pluggable.php on line 1534