[英]WP Login & Password comparisons
How i comparisons password & login via ajax using wp functions ? 我如何通过使用wp函数的ajax比较密码和登录? If user enter wrong password show error msj else ok.
如果用户输入错误密码显示错误msj,否则确定。 I try this code
我试试这段代码
$username = '';
$password = '';
$auth = wp_authenticate( $username, $password );
if ( is_wp_error( $auth ) ) {
$error_string = $auth->get_error_message();
echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
}
else {
echo "Ok!";
}
To Create the login page using ajax please follow the below steps : 要使用ajax创建登录页面,请按照以下步骤操作:
1) Create form html in any custom template OR from admin side page Pages >> Add New 1)在任何自定义模板中创建表单html或从管理员页面创建页面>>添加新
<div class="top_bar">
<div class="container">
<div class="ajax_login">
<form id="login" action="login" method="post">
<h1><?php esc_attr_e('User login','wordpress') ?></h1>
<p class="status"></p>
<input id="username" type="text" name="username" placeholder="<?php esc_attr_e('Username','wordpress') ?>">
<input id="password" type="password" name="password" placeholder="<?php esc_attr_e('Password','wordpress') ?>">
<div class="forgotten_box">
<a class="lost" href="<?php echo esc_url(wp_lostpassword_url()); ?>"><?php esc_attr_e('Lost your password?','wordpress') ?></a>
</div>
<input class="submit_button" type="submit" value="Login" name="submit">
<?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
</form>
<div class="ajax_login_overlay"></div>
<?php if (is_user_logged_in()):?>
<a class="login_button" href="<?php echo wp_logout_url( home_url() ); ?>"><?php esc_attr_e('Logout','wordpress') ?></a>
<?php else: ?>
<a class="login_button" id="show_login" href=""><?php esc_attr_e('Login','wordpress') ?></a>
<?php endif; ?>
</div>
</div>
</div>
2) Add Below Code in your current active theme functions.php file 2)在当前活动主题functions.php文件中添加以下代码
For More Help see this link : signon and Ajax Call 有关更多帮助,请参阅此链接: 登录和Ajax呼叫
/********* AJAX Login ***********/
function yourtheme_ajax_login_init(){
wp_register_script('ajax-login-script', get_template_directory_uri() . '/js/ajax-login-script.js', array('jquery') );
wp_enqueue_script('ajax-login-script');
wp_localize_script( 'ajax-login-script', 'ajax_login_object', array(
'ajaxurl' => admin_url( 'admin-ajax.php' ),
'redirecturl' => home_url(),
'loadingmessage' => esc_html__('Sending user info, please wait...', 'wordpress')
));
// Enable the user with no privileges to run ajax_login() in AJAX
add_action( 'wp_ajax_nopriv_ajaxlogin', 'ajax_login' );
}
// Execute the action only if the user isn't logged in
if (!is_user_logged_in()) {
add_action('init', 'yourtheme_ajax_login_init');
}
if (!function_exists('ajax_login')) {
function ajax_login(){
// First check the nonce, if it fails the function will break
check_ajax_referer( 'ajax-login-nonce', 'security' );
// Nonce is checked, get the POST data and sign user on
$info = array();
$info['user_login'] = $_POST['username'];
$info['user_password'] = $_POST['password'];
$info['remember'] = true;
$user_signon = wp_signon( $info, false );
if ( is_wp_error($user_signon) ){
echo json_encode(array('loggedin'=>false, 'message'=> esc_html__('Wrong username or password.', 'wordpress'))); //Send Error Message on login Failed.
} else {
echo json_encode(array('loggedin'=>true, 'message'=> esc_html__('Login successful, redirecting...', 'wordpress'))); //Send Message on Login Success .
}
wp_die();
}
}
3) En-queue the custom js file ajax-login-script.js
that we placed in the js/ folder, write the below code in functions.php file 3)对我们放在js /文件夹中的自定义js文件
ajax-login-script.js
进行排队,在functions.php文件中写下面的代码
jQuery(document).ready(function($) {
"use strict";
// Show the login dialog box on click
$('a#show_login').on('click', function(e){
$('.ajax_login_overlay').fadeIn(500);
$('form#login').fadeIn(500);
e.preventDefault();
});
$('.ajax_login_overlay').on('click', function(e){
$('form#login').fadeOut(500);
$('.ajax_login_overlay').fadeOut(500);
$('form#register_form').hide();
$('.ajax_login .status').html('');
$('#registration-error-message').html('');
$('form#login').hide();
$('form#register_form .field input').val('');
});
// Perform AJAX login on form submit
$('form#login').on('submit', function(e){
$('form#login p.status').show().text(ajax_login_object.loadingmessage);
$.ajax({
type: 'POST',
dataType: 'json',
url: ajax_login_object.ajaxurl,
data: {
'action': 'ajaxlogin', //calls wp_ajax_nopriv_ajaxlogin
'username': $('form#login #username').val(),
'password': $('form#login #password').val(),
'security': $('form#login #security').val() },
success: function(data){
$('form#login p.status').text(data.message);
if (data.loggedin === true){
document.location.href = ajax_login_object.redirecturl;
}
}
});
e.preventDefault();
});
});
I hope it will help you. 我希望它会对你有所帮助。
Try below code 试试下面的代码
$user = get_user_by( 'login', $username );
if($user && wp_check_password($pass, $user->data->user_pass, $user->ID)){
echo "That's it";
}
else{
echo "Nope";
}
Ref : https://codex.wordpress.org/Function_Reference/wp_check_password 参考: https : //codex.wordpress.org/Function_Reference/wp_check_password
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.