没有插件的AJAX登录不起作用。在将操作添加到unction.php时

时间:2015-04-08 作者:mixer

我使用的是wordpress ajax登录,没有插件。

我希望不允许用户在登录后访问WordPress配置文件,因为我在链接中设计了一个特定的配置文件:http://www.domain.com/profile. 这就是我使用以下代码的原因。当我向函数添加以下代码时。php。AJAX登录不起作用。

  // Redirect user to profile.php page 
add_action(\'init\' , \'prevent_profile_access\');
function prevent_profile_access(){
    if (current_user_can(\'manage_options\')) return \'\';

    if (strpos($_SERVER [\'REQUEST_URI\'] , \'wp-admin\' )){
        wp_redirect ("http://www.domain.com/profile");
    }
    if (strpos($_SERVER [\'REQUEST_URI\'] , \'wp-login.php\' )){
        wp_redirect ("http://www.domain.com/profile");
    }
}
我如何解决这个问题?

我的AJAX登录代码:

HTML:

<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(function.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(AJAX登录脚本.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();
    });

});

1 个回复
SO网友:Spyros

你以后需要把它挂起来。

add_action(\'template_redirect\' , \'prevent_profile_access\');
参考文献:

https://codex.wordpress.org/Plugin_API/Action_Referencehttps://codex.wordpress.org/Plugin_API/Action_Reference/template_redirect

结束

相关推荐

致命错误:使用AJAX调用未定义的函数get_post()

我有一个index.php 带有about链接的页面,可以从WordPress仪表板中创建的名为about的页面(帖子)获取作者内容。我正在使用Magnific Popup poups插件。我有about.php 其中包括以下关于页面的内容:<?php $pageid = 2; // 2 is the id of about page/post $about = get_post($pageid); ?> <div id=\"custo