我如何使用wp函数通过ajax比较密码和登录名?如果用户输入了错误的密码,则显示错误msj,否则就可以。我尝试这段代码
$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!";
}
答案 0 :(得分:1)
要使用ajax创建登录页面,请执行以下步骤:
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)在您当前的活动主题functions.php文件中添加以下代码
有关更多帮助,请参见以下链接:signon和Ajax Call
/********* 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)使我们放入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();
});
});
希望它能对您有所帮助。
答案 1 :(得分:0)
尝试下面的代码
import numpy as np
a = np.ones((2,3), dtype=int)
b = np.random.random((2,3))
a += b
Ref:https://codex.wordpress.org/Function_Reference/wp_check_password