WordPress使用Ajax注销登录

4

我可以通过ajax登出wordpress吗?你不能使用wp-login.php,所以我们需要使用admin-ajax.php。我正在使用以下代码:

html(小部件):

            <form id="logout" action="logout" method="post">
            <input class="submit_button" type="submit" value="<?php echo $lg_logout[$lang] ?>" name="submit">
            <?php wp_nonce_field( 'ajax-logout-nonce', 'logoutsecurity' ); ?>
            </form>

functions.php

add_action('init', 'ajax_login_init');
function ajax_logout_init(){
    add_action( 'wp_ajax_ajaxlogout', 'ajax_logout' );
}
add_action('init', 'ajax_logout_init');
function ajax_logout(){
check_ajax_referer( 'ajax-logout-nonce', 'logoutsecurity' );
// kill session
wp_clear_auth_cookie();
wp_logout();
die();
}

和Ajax(JavaScript)相关:
$('form#logout').on('submit', function(e){
    $.ajax({
        type: 'POST',
        url: siteUrl+'/wp-admin/admin-ajax.php',
        data: { 
            'action': 'ajaxlogout', //calls wp_ajax_nopriv_ajaxlogout
            'logoutsecurity': $('form#logout #logoutsecurity').val() },
        success: function(data){
            console.log('tutu');
                //relodlognwidget();
        }
    });
    e.preventDefault();
});

有什么问题吗?


你刷新了页面吗?可能已经注销了,但显然因为你正在使用ajax进行操作,所以页面在刷新之前不会改变。 - David
代码现在可以工作了,我犯了一个错误。如果你使用它,用jQuery的load函数,你可以通过relodlognwidget()这样的函数刷新小部件 :) - zorg
1个回答

7

感谢 @zorg 的开端。我看到你已经使它工作了。对于像我一样需要 ajax 注销的人,这里提供一个完整的解决方案。

/** Set up the Ajax Logout */
if (is_admin()) {
    // We only need to setup ajax action in admin.
    add_action('wp_ajax_custom_ajax_logout', 'custom_ajax_logout_func');
} else {
    wp_enqueue_script('custom-ajax-logout', get_stylesheet_directory_uri() . '/js/customAjaxLogout.js', array('jquery'), '1.0', true );
    wp_localize_script('custom-ajax-logout', 'ajax_object',
        array(
            'ajax_url' => admin_url('admin-ajax.php'),
            'home_url' => get_home_url(),
            'logout_nonce' => wp_create_nonce('ajax-logout-nonce'),
        )
    );
}
function custom_ajax_logout_func(){
    check_ajax_referer( 'ajax-logout-nonce', 'ajaxsecurity' );
    wp_logout();
    ob_clean(); // probably overkill for this, but good habit
    wp_send_json_success();
}

现在让我们来看一下JavaScript文件。如果您使用了上面的代码,它将被保存在活动主题文件夹中,就像这样:wp-content/themes/yourtheme/js/customAjaxLogout.js 注意:我正在使用jQuery.on的形式,它允许在JavaScript文件运行之后创建注销按钮。由于我们有ajax注销,我们可能会使用Ajax加载其他内容,甚至是注销按钮。

$(document).on('click','.logout', function(e) {
    e.preventDefault();
    $.ajax({
        type: 'POST',
        url: ajax_object.ajax_url,
        data: {
            'action': 'custom_ajax_logout', //calls wp_ajax_nopriv_ajaxlogout
            'ajaxsecurity': ajax_object.logout_nonce
        },
        success: function(r){
            // When the response comes back
            window.location = ajax_object.home_url;
        }
    });
});

4
请注意,FYI wp_logout已经运行wp_clear_auth_cookie(),不需要再添加。您也可以使用wp_send_json_success()代替echo 'adios!!'和wp_die();。参考文献:https://codex.wordpress.org/Function_Reference/wp_send_json_success - Scott

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接