// Handle Elementor registration form submission
add_action('elementor_pro/forms/new_record', function ($record, $handler) {
$form_name = $record->get_form_settings('form_name');
if ('registration_form' !== $form_name) {
return;
}
// Get form data
$raw_fields = $record->get('fields');
$fields = [];
foreach ($raw_fields as $id => $field) {
$fields[$id] = sanitize_text_field($field['value']);
}
$first_name = $fields['name'] ?? '';
$email = sanitize_email($fields['email'] ?? '');
$phone = $fields['phone'] ?? '';
if (empty($email)) {
return;
}
$username = sanitize_user(current(explode('@', $email)));
$password = wp_generate_password();
$user_id = wp_create_user($username, $password, $email);
if (!is_wp_error($user_id)) {
wp_update_user([
'ID' => $user_id,
'first_name' => $first_name,
'role' => 'customer'
]);
// Store approval status
update_user_meta($user_id, 'customer_approved', 0);
// Save phone in billing & shipping details
update_user_meta($user_id, 'billing_phone', $phone);
update_user_meta($user_id, 'shipping_phone', $phone);
// Notify admin
wp_mail(
get_option('admin_email'),
'New Customer User Pending Approval',
"A new Customer user has registered and is pending approval:\n\nName: $first_name\nEmail: $email\nPhone: $phone\n\nApprove Now: " . admin_url('users.php')
);
}
}, 10, 2);
// Add approval column
add_filter('manage_users_columns', function ($columns) {
$columns['customer_approved'] = 'Customer Approved';
return $columns;
});
// Column content + Approve button
add_filter('manage_users_custom_column', function ($output, $column_name, $user_id) {
if ($column_name === 'customer_approved') {
$approved = get_user_meta($user_id, 'customer_approved', true);
if ($approved == 1) {
return '<span style="color:green;font-weight:bold;">Approved</span>';
} else {
$approve_url = wp_nonce_url(add_query_arg([
'action' => 'approve_customer_user',
'user_id' => $user_id,
]), 'approve_customer_user_' . $user_id);
return '<a href="' . esc_url($approve_url) . '" class="button">Approve</a>';
}
}
return $output;
}, 10, 3);
// Approval action
add_action('admin_init', function () {
if (isset($_GET['action'], $_GET['user_id']) && $_GET['action'] === 'approve_customer_user') {
$user_id = intval($_GET['user_id']);
if (wp_verify_nonce($_GET['_wpnonce'], 'approve_customer_user_' . $user_id)) {
update_user_meta($user_id, 'customer_approved', 1);
// Send approval email with reset password link
$user_info = get_userdata($user_id);
$reset_link = wp_lostpassword_url();
wp_mail(
$user_info->user_email,
'Your Account Has Been Approved',
"Hi {$user_info->first_name},\n\nYour account has been approved.\nYou can set your password here: $reset_link\n\nThank you!"
);
wp_redirect(remove_query_arg(['action', 'user_id', '_wpnonce']));
exit;
}
}
});
// Prevent login until approved
add_filter('authenticate', function ($user, $username, $password) {
if ($user instanceof WP_User) {
$approved = get_user_meta($user->ID, 'customer_approved', true);
if ($approved != 1) {
return new WP_Error(
'approval_pending',
__('Your account is pending approval. Please wait for admin approval.', 'textdomain')
);
}
}
return $user;
}, 30, 3);
// Block password reset for unapproved users
add_action('validate_password_reset', function ($errors, $user) {
if ($user && !is_wp_error($user)) {
$approved = get_user_meta($user->ID, 'customer_approved', true);
if ($approved != 1) {
$errors->add('approval_pending', __('Your account is pending approval. You cannot reset your password yet.', 'textdomain'));
}
}
}, 10, 2);
// Also block the 'Set New Password' screen
add_action('login_form_rp', 'block_unapproved_password_reset');
add_action('login_form_resetpass', 'block_unapproved_password_reset');
function block_unapproved_password_reset() {
$user = check_password_reset_key($_REQUEST['key'] ?? '', $_REQUEST['login'] ?? '');
if ($user instanceof WP_User) {
$approved = get_user_meta($user->ID, 'customer_approved', true);
if ($approved != 1) {
wp_die(__('Your account is pending approval. You cannot set a new password yet.', 'textdomain'));
}
}
}
registration_form is the form name