Skip to content

Instantly share code, notes, and snippets.

@yousufansa
Created July 11, 2019 05:59
Show Gist options
  • Save yousufansa/bb24e90e1f9697db80d667edc3515ca9 to your computer and use it in GitHub Desktop.
Save yousufansa/bb24e90e1f9697db80d667edc3515ca9 to your computer and use it in GitHub Desktop.
Jobhunt - Login User Redirect to Woocommerce Dashboard ( roles except employer and candidate )
// logs a member in after submitting a form
if ( ! function_exists( 'jobhunt_login_member' ) ) {
function jobhunt_login_member() {
if( isset( $_POST['jobhunt_login_check'] ) && wp_verify_nonce( $_POST['jobhunt_login_nonce'], 'jobhunt-login-nonce') ) {
// this returns the user ID and other info from the user name
if ( is_email( $_POST['jobhunt_user_login'] ) ) {
$user = get_user_by( 'email', $_POST['jobhunt_user_login'] );
} else {
$user = get_user_by( 'login', $_POST['jobhunt_user_login'] );
}
if( ! $user ) {
// if the user name doesn't exist
jobhunt_form_errors()->add('empty_username', esc_html__('Invalid username or email address','jobhunt'));
}
if( ! isset($_POST['jobhunt_user_pass']) || $_POST['jobhunt_user_pass'] == '' ) {
// if no password was entered
jobhunt_form_errors()->add('empty_password', esc_html__('Please enter a password','jobhunt'));
}
if( isset( $_POST['jobhunt_user_pass'] ) && ! empty( $_POST['jobhunt_user_pass'] ) ){
// check the user's login with their password
if( ! wp_check_password( $_POST['jobhunt_user_pass'], $user->user_pass, $user->ID ) ) {
// if the password is incorrect for the specified user
jobhunt_form_errors()->add('empty_password', esc_html__('Incorrect password','jobhunt'));
}
}
// retrieve all error messages
$errors = jobhunt_form_errors()->get_error_messages();
// only log the user in if there are no errors
if( empty( $errors ) ) {
$creds = array();
$creds['user_login'] = $user->user_login;
$creds['user_password'] = $_POST['jobhunt_user_pass'];
$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( jobhunt_is_woocommerce_activated() && get_option( 'woocommerce_myaccount_page_id' ) && apply_filters( 'jobhunt_employer_myaccount_redirect', false ) ) {
$job_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
} elseif( 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( jobhunt_is_woocommerce_activated() && get_option( 'woocommerce_myaccount_page_id' ) && apply_filters( 'jobhunt_candidate_myaccount_redirect', false ) ) {
$resume_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
} elseif( 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( '/' );
}
if( jobhunt_is_woocommerce_activated() && get_option( 'woocommerce_myaccount_page_id' ) && apply_filters( 'jobhunt_default_myaccount_redirect', true ) ) {
$default_url = get_permalink( get_option('woocommerce_myaccount_page_id') );
} else {
$default_url= home_url( '/' );
}
switch( $sRole ) {
case 'candidate':
$redirect_url = $resume_url;
break;
case 'employer':
$redirect_url = $job_url;
break;
default:
$redirect_url = $default_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