I am working on a wordpress job portal. The problem that I am facing is that. The functionality of the registration and login form are; when a user registers, an email is generated to their registered email id with the password for their account.And the the registered user has to login using those credentials, but after every user registers for the site, the site directly logs the user in. I want the site to redirect to home or specific page after a user registers for the first time.
Registration Function
function workscout_registration_form() {
// only show the registration form to non-logged-in members
if(!is_user_logged_in()) {
// check to make sure user registration is enabled
$registration_enabled = get_option('users_can_register');
// only show the registration form if allowed
if($registration_enabled) { ?>
<form method="post" class="register workscout_form">
<?php do_action( 'woocommerce_register_form_start' ); ?>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_username' ) ) : ?>
<p class="form-row form-row-wide">
<label for="reg_username"><?php _e( 'Username', 'workscout' ); ?> <span class="required">*</span>
<i class="ln ln-icon-Male"></i>
<input type="text" class="input-text" name="username" id="reg_username" value="<?php if ( ! empty( $_POST['username'] ) ) echo esc_attr( $_POST['username'] ); ?>" />
</label>
</p>
<?php endif; ?>
<p class="form-row form-row-wide">
<label for="reg_email"><?php _e( 'Email address', 'workscout' ); ?> <span class="required">*</span>
<i class="ln ln-icon-Mail"></i><input type="email" class="input-text" name="email" id="reg_email" value="<?php if ( ! empty( $_POST['email'] ) ) echo esc_attr( $_POST['email'] ); ?>" />
</label>
</p>
<?php if ( 'no' === get_option( 'woocommerce_registration_generate_password' ) ) : ?>
<p class="form-row form-row-wide">
<label for="reg_password"><?php _e( 'Password', 'workscout' ); ?> <span class="required">*</span>
<i class="ln ln-icon-Lock-2"></i><input type="password" class="input-text" name="password" id="reg_password" />
</label>
</p>
<?php endif; ?>
<!-- Spam Trap -->
<div style="<?php echo ( ( is_rtl() ) ? 'right' : 'left' ); ?>: -999em; position: absolute;"><label for="trap"><?php _e( 'Anti-spam', 'workscout' ); ?></label><input type="text" name="email_2" id="trap" tabindex="-1" /></div>
<?php do_action( 'woocommerce_register_form' ); ?>
<?php do_action( 'register_form' ); ?>
<p class="form-row">
<?php wp_nonce_field( 'woocommerce-register' ); ?>
<input type="submit" class="button" name="register" value="<?php esc_attr_e( 'Register', 'workscout' ); ?>" />
</p>
<?php do_action( 'woocommerce_register_form_end' ); ?>
</form>
<?php } else {
_e('User registration is not enabled','workscout');
}
}
}
add_shortcode('workscout_register_form', 'workscout_registration_form');
I don't have much knowledge on php, I am little confused between the functions I have added here.
function wc_custom_user_redirect( $redirect, $user ) {
// Get the first of all the roles assigned to the user
$role = $user->roles[0];
$dashboard = admin_url();
$myaccount = get_permalink( wc_get_page_id( 'myaccount' ) );
if( $role == 'employer' ) {
if(get_option( 'job_manager_job_dashboard_page_id')) {
$redirect_to = get_permalink(get_option( 'job_manager_job_dashboard_page_id'));
} else {
$redirect_to= home_url();
};
} elseif ( $role == 'candidate' ) {
if(get_option( 'resume_manager_candidate_dashboard_page_id')) {
$redirect_to = get_permalink(get_option( 'resume_manager_candidate_dashboard_page_id'));
} else {
$redirect_to= home_url();
};
} elseif ( $role == 'customer' || $role == 'subscriber' ) {
//Redirect customers and subscribers to the "My Account" page
$redirect = $myaccount;
} else {
//Redirect any other role to the previous visited page or, if not available, to the home
$redirect = wp_get_referer() ? wp_get_referer() : home_url();
}
return $redirect;
}
add_filter( 'woocommerce_login_redirect', 'wc_custom_user_redirect', 10, 2 );
Do i have to added some redirect code in this function, I tried commenting and adding a redirection code, but it didn't work
if ( ! function_exists( 'workscout_add_new_member' ) ) :
function workscout_add_new_member() {
$login_system = Kirki::get_option( 'workscout', 'pp_login_form_system' );
if($login_system == 'workscout') {
if (isset( $_POST["workscout_register_check"] ) && wp_verify_nonce($_POST['workscout_register_nonce'], 'workscout-register-nonce')) {
if ( !isset($_POST['confirm_email']) || $_POST['confirm_email'] !== '' ) {
home_url( '/' );
exit;
}
$user_login = $_POST["workscout_user_login"];
$user_email = $_POST["workscout_user_email"];
$user_role = $_POST["workscout_user_role"];
if(username_exists($user_login)) {
// Username already registered
workscout_form_errors()->add('username_unavailable', __('Username already taken','workscout'));
}
if(!validate_username($user_login)) {
// invalid username
workscout_form_errors()->add('username_invalid', __('Invalid username','workscout'));
}
if($user_login == '') {
// empty username
workscout_form_errors()->add('username_empty', __('Please enter a username','workscout'));
}
if(!is_email($user_email)) {
//invalid email
workscout_form_errors()->add('email_invalid', __('Invalid email','workscout'));
}
if(email_exists($user_email)) {
//Email address already registered
workscout_form_errors()->add('email_used', __('Email already registered','workscout'));
}
$password = wp_generate_password();
$password_generated = true;
if(empty($user_role)) {
$user_role = 'candidate';
}
$errors = workscout_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,
)
);
if($new_user_id) {
// send an email to the admin alerting them of the registration
//
if (function_exists('sb_we_init')) {
wp_new_user_notification( $new_user_id, null, 'both' );
} else {
workscout_wp_new_user_notification($new_user_id,$password);
}
// 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 $user->get_error_message();
} else {
wp_safe_redirect( add_query_arg( 'success', 1, home_url( '/' ) ) );
}
exit;
}
}
}
}
}
endif;
add_action('init', 'workscout_add_new_member');
I found this additional code. Do you think home url should be added here ?
function workscout_ajax_register(){
check_ajax_referer( 'ajax-register-nonce', 'security' );
// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_login'] = sanitize_user($_POST['username']);
$info['user_email'] = sanitize_email( $_POST['email']);
$info['user_role'] = sanitize_email( $_POST['role']);
$valid = true;
if(username_exists( $info['user_login'])) {
// Username already registered
echo json_encode(array('loggedin'=>false, 'message'=>__('Username already taken','workscout'), 'type'=>"error notification " ));
$valid = false;
die();
}
if(!validate_username( $info['user_login'])) {
// invalid username
echo json_encode(array('loggedin'=>false, 'message'=>__('Invalid username','workscout'), 'type'=>"error notification " ));
$valid = false;
die();
}
if( $info['user_login'] == '') {
// empty username
echo json_encode(array('loggedin'=>false, 'message'=>__('Please enter a username','workscout'), 'type'=>"error notification " ));
$valid = false;
die();
}
if(!is_email($info['user_email'])) {
//invalid email
echo json_encode(array('loggedin'=>false, 'message'=>__('Invalid email','workscout'), 'type'=>"error notification " ));
$valid = false;
die();
}
if(email_exists($info['user_email'])) {
//Email address already registered
echo json_encode(array('loggedin'=>false, 'message'=>__('Email already registered','workscout'), 'type'=>"error notification " ));
$valid = false;
die();
}
$password = wp_generate_password();
$password_generated = true;
$info['user_pass'] = $password;
// Register the user
if($valid) {
$user_register = wp_insert_user( $info );
if ( is_wp_error($user_register) ){
$error = $user_register->get_error_codes() ;
if(in_array('empty_user_login', $error))
echo json_encode(array('loggedin'=>false, 'message'=>__($user_register->get_error_message('empty_user_login')), 'type'=>"error notification " ));
elseif(in_array('existing_user_login',$error))
echo json_encode(array('loggedin'=>false, 'message'=>__('This username is already registered.','workscout'), 'type'=>"error notification " ));
elseif(in_array('existing_user_email',$error))
echo json_encode(array('loggedin'=>false, 'message'=> __('This email address is already registered.','workscout'), 'type'=>"error notification " ));
} else {
if (function_exists('sb_we_init')) {
wp_new_user_notification( $user_register, null, 'both' );
} else {
workscout_wp_new_user_notification($user_register,$password);
}
$creds = array();
$creds['user_login'] = $info['user_login'];
$creds['user_password'] = $password;
$creds['remember'] = true;
$user_signon = wp_signon( $creds, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=>__('Wrong username or password.','workscout'), 'type'=>"error notification " ));
} else {
wp_set_current_user($user_signon->ID);
$oUser = get_user_by( 'login', $creds['user_login'] );
$aUser = get_object_vars( $oUser );
$sRole = $aUser['roles'][0];
echo json_encode(array('loggedin'=>true, 'message'=>__('Registration successful, redirecting...','workscout'), 'type'=>'success notification ', 'role'=> $sRole ));
}
}
}
die();
}
endif;
?>