I edited my WordPress site custom registration page. I’ve added 2 new user roles:
- Employer
- Agent
I renamed default WordPress Subscriber
role to Job Seeker
. And, this Job Seeker
role has all the default WordPress Subscriber
‘s capabilities.
Inside my Registration (template-register.php
) page, I show an input field for the user that select Job Seeker as their role. This input field ID is referralAgent
.
If they are Job Seeker
, they will see this input field and need to enter Referral Agent username.
If they are Agent
or Employer
, this Referral Agent input field will be hidden to them.
I successfully added a Job Seeker
user and record the user’s Referral Agent data. But, when I try to register as an Agent
or Employer
user, it’s failed. My submit button just loading for a while, nothing happened and all fields hold the entered values (they are not resetting themselves).
This is my code that related to the sign-up process inside my functions.php
file:
function wpjobusRegisterForm() {
if ( isset( $_POST['wpjobusRegister_nonce'] ) && wp_verify_nonce( $_POST['wpjobusRegister_nonce'], 'wpjobusRegister_html' ) ) {
$username = sanitize_text_field($_POST['userName']);
$email = sanitize_email($_POST['userEmail']);
$password = $_POST['userPassword'];
$role = $_POST['userRole'];
$ref = $_POST['referralAgent'];
$registerSuccess = 1;
$registerName = 1;
$registerEmail = 1;
if (username_exists($username)) {
$registerSuccess = 0;
$registerName = 0;
}
if( email_exists( $email )) {
$registerSuccess = 0;
$registerEmail = 0;
}
// check if agent is in database or not
if ( username_exists( $ref ) ){
// agent exist
}
else {
// agent is not exist
$registerSuccess = 0;
$registerRef = 0;
}
if($registerName == 0 AND $registerEmail == 0) {
// email & username already exist
$registerUserSuccess = 3;
} elseif($registerEmail == 0) {
// email already exist
$registerUserSuccess = 2;
} elseif($registerName == 0) {
// username already exist
$registerUserSuccess = 1;
}
elseif($registerRef == 0) {
// agent already exist
$registerUserSuccess = 6;
}
if($registerSuccess == 1) {
// if no validation errors, save & register the user
$user_id = wp_create_user( $username, $password, $email );
// Set the role
$user = new WP_User( $user_id );
$user->set_role( $role );
// Set the referral agent data IF HAVE referral agent
if ($role == 'subscriber') {
add_user_meta( $user_id, 'referral_agent', $ref);
}
// Send email to user after they register
$from = get_option('admin_email');
$headers = "From: FindWorkers <".$from.">rn";
$subject = "Registration successful";
$msg = "Registration successful.nnYour login detailsnUsername: $usernamenPassword: $passwordnnYou can now login at: http://www.findworkers.my/login/ nnThanks.n";
wp_mail( $email, $subject, $msg, $headers );
// end
/*
// Send email to admin when receive a registration request
$to = get_option('admin_email'); // admin email
$from = $email; // user email
$headers = 'From: '.$from . "rn";
$subject = "New User Registration";
$msg = "New user registered on your site.nnThe user login detailsnUsername: $usernamennPlease login to Users area and approve or disapprove this user.nnThanks.n";
wp_mail( $to, $subject, $msg, $headers );
// end
*/
$login_data = array();
$login_data['user_login'] = $username;
$login_data['user_password'] = $password;
wp_signon( $login_data, false );
$registerUserSuccess = 4;
}
} else {
$registerUserSuccess = 5;
}
echo $registerUserSuccess;
die(); // this is required to return a proper result
}
add_action( 'wp_ajax_wpjobusRegisterForm', 'wpjobusRegisterForm' );
add_action( 'wp_ajax_nopriv_wpjobusRegisterForm', 'wpjobusRegisterForm' );
And this is my template_register.php
file:
<?php
/**
* Template name: Register Page
*/
if ( is_user_logged_in() ) {
$profile = home_url()."/my-account";
wp_redirect( $profile ); exit;
}
$page = get_page($post->ID);
$current_page_id = $page->ID;
get_header(); ?>
<section id="blog">
<div class="container">
<div class="resume-skills">
<h1 class="resume-section-title"><i class="fa fa-check"></i><?php _e( 'Register an Account', 'agrg' ); ?></h1>
<h3 class="resume-section-subtitle"><?php _e( 'Youâll be able to post your resume, apply for a job, add companies profiles and post job offers!', 'agrg' ); ?></h3>
<div class="divider"></div>
<div class="full">
<?php
if(get_option('users_can_register')) { //Check whether user registration is enabled by the administrator
?>
<div class="one_half first">
<form id="wpjobus-register" type="post" action="" >
<span class="one_half first">
<h3><?php _e( 'Username:', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<input type="text" name="userName" id="userName" value="" class="input-textarea" placeholder="" />
<label for="userName" class="error userNameError"></label>
</span>
<span class="one_half first">
<h3><?php _e( 'Email:', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<input type="text" name="userEmail" id="userEmail" value="" class="input-textarea" placeholder="" />
<label for="userEmail" class="error userEmailError"></label>
</span>
<span class="one_half first">
<h3><?php _e( 'Role:', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<select name="userRole" id="userRoleDropdown">
<option>-- Select Role --</option>
<option value="subscriber" id="jobSeekerOption">Job Seeker</option>
<option value="agent">Agent</option>
<option value="employer">Employer</option>
</select>
<label for="userRole" class="error userRoleError"></label>
</span>
<div id="referralAgentSection" style="display:none;">
<span class="one_half first">
<h3><?php _e( 'Referral Agent (if any):', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<input type="text" name="referralAgent" id="referralAgent" value="" class="input-textarea" placeholder="" />
<label for="referralAgent" class="error referralAgentError"></label>
</span>
</div>
<span class="one_half first">
<h3><?php _e( 'Password:', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<input type="password" name="userPassword" id="userPassword" value="" class="input-textarea" placeholder="" />
</span>
<span class="one_half first">
<h3><?php _e( 'Repeat Password:', 'agrg' ); ?></h3>
</span>
<span class="one_half">
<input type="password" name="userConfirmPassword" id="userConfirmPassword" value="" class="input-textarea" placeholder="" />
</span>
<input type="hidden" name="action" value="wpjobusRegisterForm" />
<?php wp_nonce_field( 'wpjobusRegister_html', 'wpjobusRegister_nonce' ); ?>
<input style="margin-bottom: 0;" name="submit" type="submit" value="<?php _e( 'Register', 'agrg' ); ?>" class="input-submit">
<span class="submit-loading"><i class="fa fa-refresh fa-spin"></i></span>
</form>
<div id="success">
<span>
<h3><?php _e( 'Registration successful.', 'agrg' ); ?></h3>
</span>
</div>
<div id="error">
<span>
<h3><?php _e( 'Something went wrong, try refreshing and submitting the form again.', 'agrg' ); ?></h3>
</span>
</div>
<script type="text/javascript">
jQuery(function($) {
$('#userRoleDropdown').change(function () {
var selectedId = $('option:selected', this).attr('id');
if (selectedId == "jobSeekerOption") {
// show the result box
$('#referralAgentSection').show("slow", "swing");
}
else{
// hide the result box
$('#referralAgentSection').hide("slow", "swing");
}
});
jQuery('#wpjobus-register').validate({
rules: {
userName: {
required: true,
minlength: 3
},
userEmail: {
required: true,
email: true
},
userPassword: {
required: true,
minlength: 6,
},
userConfirmPassword: {
required: true,
minlength: 6,
equalTo: "#userPassword"
},
userRole: {
required: true
}
},
messages: {
userName: {
required: "<?php _e( 'Please provide a username', 'agrg' ); ?>",
minlength: "<?php _e( 'Your username must be at least 3 characters long', 'agrg' ); ?>"
},
userEmail: {
required: "<?php _e( 'Please provide an email address', 'agrg' ); ?>",
email: "<?php _e( 'Please enter a valid email address', 'agrg' ); ?>"
},
userPassword: {
required: "<?php _e( 'Please provide a password', 'agrg' ); ?>",
minlength: "<?php _e( 'Your password must be at least 6 characters long', 'agrg' ); ?>"
},
userConfirmPassword: {
required: "<?php _e( 'Please provide a password', 'agrg' ); ?>",
minlength: "<?php _e( 'Your password must be at least 6 characters long', 'agrg' ); ?>",
equalTo: "<?php _e( 'Please enter the same password as above', 'agrg' ); ?>"
},
userRole: {
required: "<?php _e( 'Please select your role', 'agrg' ); ?>"
}
},
submitHandler: function(form) {
jQuery('#wpjobus-register .input-submit').css('display','none');
jQuery('#wpjobus-register .submit-loading').css('display','block');
jQuery(form).ajaxSubmit({
type: "POST",
data: jQuery(form).serialize(),
url: '<?php echo admin_url('admin-ajax.php'); ?>',
success: function(data) {
if(data == 1) {
jQuery("#userName").addClass("error");
jQuery(".userNameError").text("<?php _e( 'Username exists. Please try another one.', 'agrg' ); ?>");
jQuery('.userNameError').css('display','block');
jQuery('#wpjobus-register .input-submit').css('display','block');
jQuery('#wpjobus-register .submit-loading').css('display','none');
}
if(data == 2) {
jQuery("#userEmail").addClass("error");
jQuery(".userEmailError").text("<?php _e( 'Email exists. Please try another one.', 'agrg' ); ?>");
jQuery('.userEmailError').css('display','block');
jQuery('#wpjobus-register .input-submit').css('display','block');
jQuery('#wpjobus-register .submit-loading').css('display','none');
}
if(data == 3) {
jQuery("#userName").addClass("error");
jQuery(".userNameError").text("<?php _e( 'Username exists. Please try another one.', 'agrg' ); ?>");
jQuery('.userNameError').css('display','block');
jQuery("#userEmail").addClass("error");
jQuery(".userEmailError").text("<?php _e( 'Email exists. Please try another one.', 'agrg' ); ?>");
jQuery('.userEmailError').css('display','block');
jQuery('#wpjobus-register .input-submit').css('display','block');
jQuery('#wpjobus-register .submit-loading').css('display','none');
}
if(data == 4) {
jQuery('#wpjobus-register :input').attr('disabled', 'disabled');
jQuery('#wpjobus-register').fadeTo( "slow", 0, function() {
jQuery('#wpjobus-register').css('display','none');
jQuery(this).find(':input').attr('disabled', 'disabled');
jQuery(this).find('label').css('cursor','default');
jQuery('#success').fadeIn();
<?php $profile = home_url()."/my-account"; ?>
var delay = 1000;
setTimeout(function(){ window.location = '<?php echo $profile; ?>';}, delay);
});
}
if(data == 6) {
jQuery("#referralAgent").addClass("error");
jQuery(".referralAgentError").text("<?php _e( 'Agent is not exists. Please try another one.', 'agrg' ); ?>");
jQuery('.referralAgentError').css('display','block');
jQuery('#wpjobus-register .input-submit').css('display','block');
jQuery('#wpjobus-register .submit-loading').css('display','none');
}
if(data == 5) {
jQuery('#wpjobus-register').fadeTo( "slow", 0, function() {
jQuery('#error').fadeIn();
});
}
},
error: function(data) {
jQuery('#wpjobus-register').fadeTo( "slow", 0, function() {
jQuery('#error').fadeIn();
});
}
});
}
});
});
</script>
</div>
<div class="one_half social-links">
<h3 style="margin-top: 0;"><?php _e( 'Social account register', 'agrg' ); ?></h3>
<?php
/**
* Detect plugin. For use on Front End only.
*/
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if ( is_plugin_active( "nextend-facebook-connect/nextend-facebook-connect.php" ) ) {
//plugin is activated
?>
<a class="register-social-button-facebook" href="<?php echo get_site_url(); ?>/wp-login.php?loginFacebook=1" onclick="window.location = '<?php echo get_site_url(); ?>/wp-login.php?loginFacebook=1&redirect='+window.location.href; return false;"><i class="fa fa-facebook-square"></i> Facebook</a>
<?php } ?>
<?php
/**
* Detect plugin. For use on Front End only.
*/
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if ( is_plugin_active( "nextend-twitter-connect/nextend-twitter-connect.php" ) ) {
//plugin is activated
?>
<a class="register-social-button-twitter" href="<?php echo get_site_url(); ?>/wp-login.php?loginTwitter=1" onclick="window.location = '<?php echo get_site_url(); ?>/wp-login.php?loginTwitter=1&redirect='+window.location.href; return false;"><i class="fa fa-twitter-square"></i> Twitter</a>
<?php } ?>
<?php
/**
* Detect plugin. For use on Front End only.
*/
include_once( ABSPATH . 'wp-admin/includes/plugin.php' );
// check for plugin using plugin name
if ( is_plugin_active( "nextend-google-connect/nextend-google-connect.php" ) ) {
//plugin is activated
?>
<a class="register-social-button-google" href="<?php echo get_site_url(); ?>/wp-login.php?loginGoogle=1" onclick="window.location = '<?php echo get_site_url(); ?>/wp-login.php?loginGoogle=1&redirect='+window.location.href; return false;"><i class="fa fa-google-plus-square"></i> Google</a>
<?php } ?>
</div>
<?php }
else echo "<span class='registration-closed'>Registration is currently disabled. Please try again later.</span>";
?>
</div>
</div>
</div>
</section>
<?php get_footer(); ?>
My questions:
- How to successfully register an user if they choose their role as an
Agent
or as anEmployer
? - How to send different sign-up email to different user role? Eg: if they are
Job Seeker
, sendemail1
. If they areAgent
, sendemail2
. And if they areEmployer
, sendemail3
. - How to set
referralAgent
field torequired
when a user choose the role as anJob Seeker
and show an error message if it isempty
using jQuery?