How to check using php, Ajax and jQuery is user logged in or is user logged out in Wordpress?
How to check using php, Ajax and jQuery is user logged in or is user logged out in Wordpress?
if you want to do it using jQuery with cookies then use this code:
function getLoggedInCookie() {
var cookie = document.cookie.indexOf('wp-settings-time') !== -1;
if(cookie){
alert('Logged in');
}else{
alert('Not User');
}
}
getLoggedInCookie();
Use this code for jQuery with ajax:
var data = {
action: 'is_user_logged_in'
};
jQuery.post(ajaxurl, data, function(response) {
if(response == 'yes') {
// user is logged in, do your stuff here
} else {
// user is not logged in, show login form here
}
});
And this code for Wordpress functions.php (using php):
function ajax_check_user_logged_in() {
echo is_user_logged_in()?'yes':'no';
die();
}
add_action('wp_ajax_is_user_logged_in', 'ajax_check_user_logged_in');
add_action('wp_ajax_nopriv_is_user_logged_in', 'ajax_check_user_logged_in');