Created
January 17, 2019 16:18
-
-
Save yousufansa/853c47d8fbb6685eb9adb52fa9037db6 to your computer and use it in GitHub Desktop.
Jobhunt - Add Custom Register Form Fields
This file contains hidden or 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
if( ! function_exists( 'jh_child_extra_register_fields' ) ) { | |
function jh_child_extra_register_fields() { | |
?> | |
<p> | |
<label for="jobhunt_register_first_name"><?php esc_html_e( 'First Name', 'jobhunt' ); ?> | |
<input name="jobhunt_first_name" id="jobhunt_register_first_name" class="required" type="text"/> | |
</label> | |
</p> | |
<p> | |
<label for="jobhunt_register_last_name"><?php esc_html_e( 'Last Name', 'jobhunt' ); ?> | |
<input name="jobhunt_last_name" id="jobhunt_register_last_name" class="required" type="text"/> | |
</label> | |
</p> | |
<?php | |
} | |
} | |
add_action( 'jobhunt_registration_form_fields_before', 'jh_child_extra_register_fields' ); | |
// register a new user function override | |
if ( ! function_exists( 'jobhunt_add_new_member' ) ) { | |
function jobhunt_add_new_member() { | |
if ( isset( $_POST["jobhunt_register_check"] ) && wp_verify_nonce( $_POST['jobhunt_register_nonce'], 'jobhunt-register-nonce' ) ) { | |
if ( ! isset( $_POST['confirm_email'] ) || $_POST['confirm_email'] !== '' ) { | |
home_url( '/' ); | |
exit; | |
} | |
$default_role = 'subscriber'; | |
$available_roles = array( 'subscriber' ); | |
if ( jobhunt_is_woocommerce_activated() ) { | |
$available_roles[] = 'customer'; | |
$default_role = 'customer'; | |
} | |
if ( jobhunt_is_wp_job_manager_activated() ) { | |
$available_roles[] = 'employer'; | |
} | |
if ( jobhunt_is_wp_resume_manager_activated() ) { | |
$available_roles[] = 'candidate'; | |
$default_role = 'candidate'; | |
} | |
$user_email = sanitize_email( $_POST["jobhunt_user_email"] ); | |
$user_role = ! empty( $_POST["jobhunt_user_role"] ) && in_array( $_POST["jobhunt_user_role"], $available_roles ) ? sanitize_text_field( $_POST["jobhunt_user_role"] ) : $default_role; | |
if ( ! empty( $_POST["jobhunt_user_login"] ) ) { | |
$user_login = sanitize_user( $_POST["jobhunt_user_login"] ); | |
} else { | |
$user_login = sanitize_user( current( explode( '@', $user_email ) ), true ); | |
// Ensure username is unique. | |
$append = 1; | |
$o_user_login = $user_login; | |
while ( username_exists( $user_login ) ) { | |
$user_login = $o_user_login . $append; | |
$append++; | |
} | |
} | |
// Customer first name. | |
if ( isset( $_POST['jobhunt_first_name'] ) ) { | |
$first_name = jobhunt_clean( $_POST['jobhunt_first_name'] ); | |
} else { | |
$first_name = ''; | |
} | |
// Customer last name. | |
if ( isset( $_POST['jobhunt_last_name'] ) ) { | |
$last_name = jobhunt_clean( $_POST['jobhunt_last_name'] ); | |
} else { | |
$last_name = ''; | |
} | |
if( username_exists( $user_login ) && apply_filters( 'jobhunt_register_user_login_enabled', true ) ) { | |
// Username already registered | |
jobhunt_form_errors()->add('username_unavailable', esc_html__('Username already taken','jobhunt')); | |
} | |
if( ! validate_username( $user_login ) && apply_filters( 'jobhunt_register_user_login_enabled', true ) ) { | |
// invalid username | |
jobhunt_form_errors()->add('username_invalid', esc_html__('Invalid username','jobhunt')); | |
} | |
if( $user_login == '' && apply_filters( 'jobhunt_register_user_login_enabled', true ) ) { | |
// empty username | |
jobhunt_form_errors()->add('username_empty', esc_html__('Please enter a username','jobhunt')); | |
} | |
if( ! is_email( $user_email ) ) { | |
//invalid email | |
jobhunt_form_errors()->add('email_invalid', esc_html__('Invalid email','jobhunt')); | |
} | |
if( email_exists( $user_email ) ) { | |
//Email address already registered | |
jobhunt_form_errors()->add('email_used', esc_html__('Email already registered','jobhunt')); | |
} | |
if( $first_name == '' && apply_filters( 'jobhunt_register_user_first_last_name_required', true ) ) { | |
// empty username | |
jobhunt_form_errors()->add('first_name_empty', esc_html__('Please enter a First Name','jobhunt')); | |
} | |
if( $last_name == '' && apply_filters( 'jobhunt_register_user_first_last_name_required', true ) ) { | |
// empty username | |
jobhunt_form_errors()->add('last_name_empty', esc_html__('Please enter a Last Name','jobhunt')); | |
} | |
$password = wp_generate_password(); | |
$password_generated = true; | |
if ( apply_filters( 'jobhunt_register_user_password_enabled', false ) && !empty( $_POST['jobhunt_user_pass'] ) ) { | |
$password = $_POST['jobhunt_user_pass']; | |
} | |
$errors = jobhunt_form_errors()->get_error_messages(); | |
// only create the user in if there are no errors | |
if( empty( $errors ) ) { | |
$new_user_id = wp_insert_user( array( | |
'user_login' => $user_login, | |
'user_pass' => $password, | |
'user_email' => $user_email, | |
'user_registered' => date('Y-m-d H:i:s'), | |
'role' => $user_role, | |
'first_name' => $first_name, | |
'last_name' => $last_name, | |
) ); | |
if( $new_user_id ) { | |
// send an email to the admin alerting them of the registration | |
wp_new_user_notification( $new_user_id, null, 'both' ); | |
// log the new user in | |
$creds = array(); | |
$creds['user_login'] = $user_login; | |
$creds['user_password'] = $password; | |
$creds['remember'] = true; | |
$user = wp_signon( $creds, false ); | |
// send the newly created user to the home page after logging them in | |
if ( is_wp_error( $user ) ) { | |
echo wp_kses_post( $user->get_error_message() ); | |
} else { | |
$oUser = get_user_by( 'login', $creds['user_login'] ); | |
$aUser = get_object_vars( $oUser ); | |
$sRole = $aUser['roles'][0]; | |
if( get_option( 'job_manager_job_dashboard_page_id' ) ) { | |
$job_url = get_permalink( get_option( 'job_manager_job_dashboard_page_id' ) ); | |
} else { | |
$job_url = home_url( '/' ); | |
} | |
if( get_option( 'resume_manager_candidate_dashboard_page_id' ) ) { | |
$resume_url = get_permalink( get_option( 'resume_manager_candidate_dashboard_page_id' ) ); | |
} else { | |
$resume_url= home_url( '/' ); | |
} | |
switch( $sRole ) { | |
case 'candidate': | |
$redirect_url = $resume_url; | |
break; | |
case 'employer': | |
$redirect_url = $job_url; | |
break; | |
default: | |
$redirect_url = home_url( '/' ); | |
break; | |
} | |
wp_safe_redirect( $redirect_url ); | |
} | |
exit; | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment