Thanks all for your help, Ibrahim youre great!!
Finally I got it to work as I wanted, so I close the post and leave what has worked for me if someone can at some point take advantage of it.
I try with the code that you provide me in Javascript:
var data = {
action: 'is_user_logged_in'
};
jQuery('.x-btn-widgetbar').on('click', function() {
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
jQuery('.x-widgetbar').hide();
window.location.href='https://website.com/login';
}
});
});
And this in functions.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');
This code does exactly what I wanted but I had a slight delay in responding by taking a few seconds to redirect and sending an alert that I did not add in the code.
I've tried what Samuel and Parthavi (thanks a lot!) wrote and put this code in Javascript:
jQuery('.x-btn-widgetbar').on('click', function() {
if(jQuery('body').hasClass('logged-in') ) {
} else {
jQuery('.x-widgetbar').hide();
window.location.href='https://website.com/login';
}
});
With this code the behavior is the same and works well, but the response is faster and prevents small delays.
If you see some way to debug the code or see something that would help me avoid problems I would appreciate it since I am not an expert in javascript.
Thanks in advance for all your help, I hope everything goes well for you.
$_POST['x-btn-widgetbar']
means and why this might or might not make any sense here?