6

我正在尝试构建一个自定义 wordpress ajax 登录表单,但我无法对其进行排序。这是我使用的代码:

HTML:

            <form class="well form-inline" id="login">
                <div class="rowmargin">
                    <h4>Login</h4>
                </div>
                <div class="rowmargin">
                    <input type="text" name="username" id="loginUsername" class="input-medium" placeholder="Username">
                    <input type="password" name="password" id="loginPassword" class="input-medium" placeholder="Password">
                </div>
                <a class="btn btn-primary" id="loginButton"><i class="icon-check icon-white"></i> Login</a>
            </form>

JS:

    <script type="text/javascript">

        $(document).ready(function() {

            $("#loginButton").click(function() {    

                var username = $('#loginUsername').val();
                var password = $('#loginPassword').val();
                var rememberme = "forever";
                var redirect = '<?php bloginfo('url'); ?>';

                var data = {
                    user_login:     username,
                    user_password:  password,
                    remember:       rememberme,
                    redirect_to:    redirect
                }

                $.ajax({
                    url: '<?php bloginfo('url'); ?>/wp-login.php',
                    data: data,
                    type: 'GET',
                    dataType: 'jsonp',
                    success: function( result ) {
                        if (result.success==1) {
                            alert("Ok!");
                        } else {
                            alert("Not Ok!");
                        }
                    }
                }); 

            });

        });

        </script> <!-- Login Script --->

有人可以告诉我我在这里做错了什么吗?

4

4 回答 4

6

WordPress:简单的 Ajax 登录表单

<form id="login" action="login" method="post">
    <h1>Site Login</h1>
    <p class="status"></p>
    <label for="username">Username</label>
    <input id="username" type="text" name="username">
    <label for="password">Password</label>
    <input id="password" type="password" name="password">
    <a class="lost" href="<?php echo wp_lostpassword_url(); ?>">Lost your password?</a>
    <input class="submit_button" type="submit" value="Login" name="submit">
    <a class="close" href="">(close)</a>
    <?php wp_nonce_field( 'ajax-login-nonce', 'security' ); ?>
</form>

--

<?php
//add this within functions.php
function ajax_login_init(){

    wp_register_script('ajax-login-script', get_template_directory_uri() . '/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' => __('Sending user info, please wait...')
    ));

    // 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', 'ajax_login_init');
}


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'=>__('Wrong username or password.')));
    } else {
        echo json_encode(array('loggedin'=>true, 'message'=>__('Login successful, redirecting...')));
    }

    die();
}

在主题目录中创建一个文件 ajax-login-script.js 并粘贴这个 js

jQuery(document).ready(function($) {

    // Show the login dialog box on click
    $('a#show_login').on('click', function(e){
        $('body').prepend('<div class="login_overlay"></div>');
        $('form#login').fadeIn(500);
        $('div.login_overlay, form#login a.close').on('click', function(){
            $('div.login_overlay').remove();
            $('form#login').hide();
        });
        e.preventDefault();
    });

    // 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();
    });

});
于 2019-12-24T10:33:54.633 回答
5

您需要使用 wp 功能进行登录。

http://codex.wordpress.org/Function_Reference/wp_signon

然后用ajax访问这个函数登录。你可以在functions.php中写一个登录函数

点击下方查看如何在 wordpress 中使用 ajax。

http://wpmu.org/how-to-use-ajax-with-php-on-your-wp-site-without-a-plugin/

于 2012-08-01T03:04:36.977 回答
1

WordPress 中的所有 AJAX 请求都必须通过wp-admin/admin-ajax.php. wp-login.php不会回应。 http://codex.wordpress.org/Class_Reference/WP_Ajax_Response

有一组可用的操作,但没有一个接近登录方法。如果您知道自己在做什么,您可以注册自己的操作并自己处理登录过程。 http://codex.wordpress.org/Plugin_API/Action_Reference/wp_ajax_(action)

于 2012-07-31T17:22:06.183 回答
0
<form class="well form-inline" id="login">
    <div id="message"></div>
    <div id="loading" style="display:none;"></div>
    <div class="rowmargin">
        <h4>Login</h4>
    </div>
    <div class="rowmargin">
        <input type="text" name="username" id="loginUsername" class="input-medium" placeholder="Username">
        <input type="password" name="password" id="loginPassword" class="input-medium" placeholder="Password">
    </div>
    <a class="btn btn-primary" id="loginButton"><i class="icon-check icon-white"></i> Login</a>
</form>


jQuery(document).ready(function(){      
    jQuery('#loading').hide();
    jQuery("#loginButton").click(function() {   
        jQuery('#message').hide().html('');
        jQuery('#loading').hide();
        var input_data = jQuery('#login').serialize();
        var logUser = jQuery('#loginUsername').val();
        var logPass = jQuery('#loginPassword').val();

        if(logUser == '' && logPass != ''){ jQuery('#message').show().html('Your Username is empty!'); return false; }
        if(logPass == '' && logUser != ''){ jQuery('#message').show().html('Your Password is empty!'); return false; }
        if(logUser == '' && logPass == ''){ jQuery('#message').show().html('Your Username and Password is empty!'); return false; } 

        jQuery('#loading').show();
        jQuery.ajax({
            type: "POST",
            url: "<?php echo site_url('wp-login.php','login_post'); ?>",
            data: input_data,
            success: function(msg) {                                    
                // login success. redirect users to some page.
                jQuery(location).attr('href', '<?php echo home_url( '/thank-you/' ); ?>');

            },
            error: function(msg) {  
                // login error.                 
                jQuery('#message').show();
                jQuery('#message').html("<?php _e('Your login  is not correct. Please try again.'); ?>");
                jQuery('#loading').hide();
            }

        }); 
        return false;
    });
});
于 2015-02-03T14:41:44.120 回答