Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to execute custom jquery code which shows login dialog to user if he clicks a button and he is not logged in. How could I do that?

share|improve this question

4 Answers

up vote 6 down vote accepted

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

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

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');
share|improve this answer
What is the value of ajaxurl? – Feras Odeh Oct 24 '12 at 19:22
wordpress sets it to 'yoursite.com/wp-admin/admin-ajax.php'. That's the url on which all the ajax requests are supposed to be sent – Mridul Aggarwal Oct 24 '12 at 19:25
I put the jQuery.post inside jquery click handler but this doesn't work. do you have any idea how to solve it? – Feras Odeh Oct 24 '12 at 19:28
I saw that ajaxurl is not defined in the console. – Feras Odeh Oct 24 '12 at 19:34
[codex](codex.wordpress.org/AJAX_in_Plugins) said that in latest wordpress versions this is already defined. Maybe you can define your own version of ajaxurl with wp_localize_script – Mridul Aggarwal Oct 24 '12 at 19:45
show 1 more comment

Check the class element for body: If the theme is using body_class() the body has a class named logged-in for users that are logged in. Be aware the function can be used on the element html too.

You can also just use is_user_logged_in() as a condition to enqueue or print the script.

share|improve this answer

Another example, in case you want to use it for AJAX calls.

// Simplified... please note, that all names/vars/etc. in my class got unique names.
// ...the same goes for the script handler.
class wpse69814_example
{
    public $response;

    public function __construct()
    {
        $this->is_user_logged_in = is_user_logged_in();

        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue' ) );
        add_action( 'wp_enqueue_scripts', array( $this, 'localize' ), 20 );
    }

    public function enqueue()
    {
        wp_enqueue_script( 'wpse69814_handler', 'url/to/file.js', array( 'jquery' ), filemtime( 'path/to/file.js' ), true );
    }

    public function localize()
    {
        wp_localize_script( 'wpse69814_handler, 'wpse69814_object', array(
             'ajaxurl'    => admin_url( 'admin-ajax.php' )
            ,'ajax_nonce' => wp_create_nonce( 'wpse69814_nonce' )
            ,'action'     => 'wpse69814-handler-action'
            ,'data'       => array(
                'is_user_logged_in' => $this->is_user_logged_in
             )
         )

    }
}
share|improve this answer

Please add body_class() to your html body

<body <?php body_class(); ?>>
   //your html code
</body>

This will add logged-in for logged user then you can use following jquery code to execute your custom juqery code only for logged user.

if ($('body').hasClass('logged-in')) {
       //execute your jquery code.
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.