Created
June 6, 2024 08:54
-
-
Save hazratbilal0079/56a8495e7f0c56485fcc9379c04164be to your computer and use it in GitHub Desktop.
Custom User Registration and Custom Role Assignment in WordPress with Meta Fields though hook
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--Hook name | |
registervisitor | |
--PHP Hook | |
add_action( 'jet-form-builder/custom-action/registervisitor', function( $request, $action_handler ) { | |
$username = sanitize_user($_POST['email']); | |
$email = sanitize_email($_POST['email']); | |
$first_name = sanitize_text_field($_POST['first-name']); | |
$user_status = sanitize_text_field($_POST['user-status']); | |
$last_name = sanitize_text_field($_POST['last-name']); | |
$password = sanitize_text_field($_POST['password']); | |
$phone_number = sanitize_text_field($_POST['phone']); | |
$designation = sanitize_text_field($_POST['designation']); | |
$companyname = sanitize_text_field($_POST['employed-at']); | |
$eiduidnumber = sanitize_text_field($_POST['eid-uid-num']); | |
$role = sanitize_text_field($_POST['register_as']); | |
$user_id = wp_create_user($email, $password, $email); | |
if (is_wp_error($user_id)) { | |
// Handle registration error here | |
echo 'User registration error: ' . $user_id->get_error_message(); | |
} else { | |
$user_id_role = new WP_User($user_id); | |
$user_id_role->set_role($role); | |
update_user_meta($user_id, 'first_name', $first_name); | |
update_user_meta($user_id, 'last_name', $last_name); | |
update_user_meta($user_id, 'phone', $phone_number); | |
update_user_meta($user_id, 'employed-at', $companyname); | |
update_user_meta($user_id, 'designation', $designation); | |
update_user_meta($user_id, 'eid-number', $eiduidnumber); | |
update_user_meta($user_id, 'user-status', $user_status); | |
} | |
}, 10, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment