add_shortcode('loginCode', 'loginCodeFun');
function loginCodeFun()
{
ob_start();
?>
<!--<script src="http://code.jquery.com/jquery-1.4.4.js"></script>--><!--如果你发现没有ajax提交,请检查是否有引入jquery.js文件-->
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<script type="text/javascript">var ajax_url = '<?php echo admin_url(); ?>admin-ajax.php';</script>
<style>
#container {
width: 550px;
margin: 0 auto;
padding: 25px;
border: 1px solid #eee;
}
#container .error{
text-align: center;
}
#submitbtn{
background-color: #C40D23;
font-size: 16px;
color: #fff;
width: 100%;
padding: 8px;
border: none;
outline: none;
}
</style>
<div id="container">
<div id="content">
<div id="result"></div>
<form id="wp_login_form" action="" method="post">
<label>用户名</label><br />
<input type="text" name="username" class="text" value="" /><br />
<label>密码</label><br />
<input type="password" name="password" class="text" value="" /> <br />
<label>
<input name="rememberme" type="checkbox" value="forever" />Remember Me</label>
<br /><br />
<input type="button" id="submitbtn" name="submit" value="Login" />
<div><a href="/lost-password/">Lost your password?</a>|<a href="/Register/">Register</a></div>
<div>Not a distributor yet?<a href="/Register/">Sign up</a></div>
</form>
<script type="text/javascript">
$("#submitbtn").click(function () {
var username = $('#wp_login_form input[name="username"]').val();
var password = $('#wp_login_form input[name="password"]').val();
// var remember = $('#wp_login_form input[name="rememberme"]').prop('checked');
var dataes = {
username:username,
password:password
}
$.ajax({
type: "POST",
url: ajax_url,
data: {
'action':'vote_post_lists',
'datas':JSON.stringify(dataes)
},
success: function (msg) {
console.log(msg);
//alert(msg.url);
if(msg.url == 1){
window.location.href = '/products/'
}
if(msg.url == 2){
window.location.href = '/distributors-only/'
}
if(msg.url == 3){
window.location.href = '/'
}
}
});
return false;
});
</script>
</div>
</div>
<?php
return ob_get_clean();
}
add_action("wp_ajax_vote_post_lists", "add_votes_list");
add_action("wp_ajax_nopriv_vote_post_lists", "add_votes_list");
function add_votes_list(){
global $user_ID;
$getdate = json_decode(str_replace("\\","",$_POST['datas']));
$login_data = array();
$login_data['user_login'] = $getdate->username;
$login_data['user_password'] = $getdate->password;
$user = wp_signon($login_data, false);
if (is_wp_error($user)) {
echo $user->get_error_message();
} else {
// wp_clear_auth_cookie();
// do_action('wp_login', $user->ID);
// wp_set_current_user($user->ID);
// wp_set_auth_cookie($user->ID, true);
// $redirect_to = $_SERVER['REQUEST_URI'];
if($user->roles[0] == 'subscriber'){
$redirect_to = 1;
}else if($user->roles[0] == 'agentmember' || $user->roles[0] == '代理商会员'){
$redirect_to =2;
}else{
$redirect_to = 3;
}
wp_send_json(array('url'=>$redirect_to));
//wp_safe_redirect($redirect_to);
exit;
}
}