How logout on wordpres using ajax ?

You can’t use wp-login.php, so we need to use admin-ajax.php

I use theses codes:
html( widget):

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

and the ajax (js):

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

What is wrong ?

Read more here: WordPress Logout using ajax


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.

Related Wordpress search:

, ,

Wordpress related questions and answers: