Frontend User Registration Provider
November 26, 2024 ยท AI Conversations
1. **A custom form for user registration on the frontend**.
2. **WordPress hooks** like `wp_insert_user` or `user_register`.
3. Assigning the **Provider** role during registration.
### Steps to Implement
#### 1. Create a Frontend Registration Form
Add a custom registration form in your theme or plugin. Here's an example of a basic form:
“`php
Username:
### 1. Hook into `user_register` to Assign the Role
The `user_register` hook runs after a new user is created. You can use it to assign the **Provider** role.
Add the following code to your theme's `functions.php` file or a custom plugin:
add_action('user_register', 'assign_provider_role', 10, 1);
function assign_provider_role($user_id) {
// Assign the 'Provider' role to the newly registered user
$user = new WP_User($user_id);
$user->set_role('provider'); // Ensure 'provider' role exists in your WordPress site
}
### 2. Create a Frontend Registration Form
Set up a frontend registration form where users can register. For example:
<form id="provider-registration-form" method="post" action="">
<label for="username">Username:</label>
<input type="text" name="username" id="username" required />
<label for="email">Email:</label>
<input type="email" name="email" id="email" required />
<label for="password">Password:</label>
<input type="password" name="password" id="password" required />
<input type="hidden" name="action" value="register_provider" />
<button type="submit">Register</button>
</form>
### 3. Handle the Form Submission
Create a function to handle the form submission, validate the inputs, and register the user. Add this to your `functions.php` file or custom plugin:
add_action('init', 'handle_provider_registration');
function handle_provider_registration() {
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['action']) && $_POST['action'] === 'register_provider') {
// Sanitize inputs
$username = sanitize_user($_POST['username']);
$email = sanitize_email($_POST['email']);
$password = sanitize_text_field($_POST['password']);
// Validate inputs
if (username_exists($username)) {
wp_die('Username already exists!');
}
if (email_exists($email)) {
wp_die('Email already registered!');
}
// Register user
$user_id = wp_create_user($username, $password, $email);
if (is_wp_error($user_id)) {
wp_die('User registration failed: ' . $user_id->get_error_message());
}
// The 'user_register' hook will automatically assign the 'provider' role
wp_redirect(home_url('/registration-success')); // Redirect after successful registration
exit;
}
}
### 4. Verify the Provider Role Exists
Ensure that the **Provider** role is defined in your WordPress installation. If not, add it using:
add_action('init', 'add_provider_role');
function add_provider_role() {
if (!get_role('provider')) {
add_role(
'provider',
'Provider',
[
'read' => true,
'edit_posts' => false,
'delete_posts' => false,
]
);
}
}
### 5. Test the Flow
1. Visit the page where your registration form is displayed.
2. Fill in the form and submit.
3. Check the WordPress admin (`Users > All Users`) to confirm the new user has the **Provider** role.
This approach uses `user_register` to assign roles, ensures secure data handling, and keeps WordPress role management flexible.