我编辑了我的WordPress网站自定义注册页面。我添加了2个新用户角色:
我将默认的WordPress Subscriber
角色重命名为Job Seeker
。而且,此Job Seeker
角色具有所有默认的WordPress Subscriber
功能。
在我的注册(template-register.php
)页面中,我显示了选择求职者作为其角色的用户的输入字段。此输入字段ID为referralAgent
。
如果他们是Job Seeker
,他们会看到此输入字段,并需要输入推介代理用户名。
如果它们是Agent
或Employer
,则会向其隐藏此引荐代理输入字段。
我成功添加了Job Seeker
用户并记录了用户的推介代理数据。但是,当我尝试注册为Agent
或Employer
用户时,它失败了。我的提交按钮只是加载了一段时间,没有任何反应,所有字段都保存输入的值(它们没有自行重置)。
这是我的代码,与我functions.php
文件中的注册过程相关:
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.">\r\n";
$subject = "Registration successful";
$msg = "Registration successful.\n\nYour login details\nUsername: $username\nPassword: $password\n\nYou can now login at: http://www.findworkers.my/login/ \n\nThanks.\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 . "\r\n";
$subject = "New User Registration";
$msg = "New user registered on your site.\n\nThe user login details\nUsername: $username\n\nPlease login to Users area and approve or disapprove this user.\n\nThanks.\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' );
这是我的template_register.php
文件:
<?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(); ?>
我的问题:
Agent
或Employer
的角色,如何成功注册用户?Job Seeker
,请发送email1
。如果他们是Agent
,请发送email2
。如果它们是Employer
,请发送email3
。referralAgent
角色时如何将required
字段设置为Job Seeker
,如果使用jQuery empty
则显示错误消息?