In case you want to know if the user is logged in at the current moment. The other answers check if the user is logged in or not when the page loaded not the time when you’re running the javascript. The user could have logged in in a seperate tab for instance
Put this in your javascript
1 2 3 4 5 6 7 8 9 10 11 | 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 } }); |
put this in your functions.php file:
1 2 3 4 5 6 7 | 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'); |
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.