0

I'm using a Wordpress plugin to create users for my site and I need to check if a user if logged in and then execute something in a jquery function. The plugin states they have public functions set up for such things. I'm not sure if I have this set up correctly, whether the user is logged in or out it is coming up "loggedout". The public function is in my functions.php file of my theme and is :

function loggedin() {
    $FEUP = new FEUP_User;
    if ($FEUP->Is_Logged_In()) {
        echo "loggedin";
    } else {
        echo "loggedout";
    }
}

my jquery function is :

var user = "<?php loggedin(); ?>";
if(user == "loggedin") {
    console.log("user is logged in");
}else {
    console.log("user is logged out");
}

This is a screenshot from the makers of the plugin as to how to implement the public function to see if the user is logged in:enter image description here

13
  • It would be nice if you provided some idea of what is wrong with the code you show. Commented Apr 18, 2017 at 15:19
  • var user = "'<?php loggedin(); ?>';"; Strings must be wrapped in quotes! Commented Apr 18, 2017 at 15:20
  • What you have should in theory work fine. It would help more if you showed the actual output of the PHP code, as for some reason it is never equal to 'loggedin' Commented Apr 18, 2017 at 15:21
  • You could do it the way @RiggsFolly suggested, or you could make a ajax request Commented Apr 18, 2017 at 15:24
  • Rather echoing the string, return the string so that var user gets initialized with the string(loggedin/notloggedin).Echo just prints the message on screen and I guess it returns true.So variable user gets abrupt value. Commented Apr 18, 2017 at 15:29

2 Answers 2

1

You might consider using wp_localize_script() to ensure that your javascript variable is being output where the jQuery code can see it.

Here is an example. This would be your php plugin code to enqueue your jQuery file:

wp_enqueue_script('my-script-handle', plugins_url( '/whatever_location/myscript.js', __FILE__), array('jquery'));

You can then add javascript variables from php using the wp_localize_script() function, which will output your variables inside script tags where your script can use them:

wp_localize_script('my-script-handle','my_plugin_vars', array('logged_status' => 'some_value'));

All together it could look like this:

PHP:

$FEUP = new FEUP_User;
$logged_status = $FEUP->Is_Logged_In() ? 'loggedin' : 'notloggedin';

wp_enqueue_script('my-script-handle', plugins_url( '/whatever_location/myscript.js', __FILE__), array('jquery'));
wp_localize_script('my-script-handle','my_plugin_vars', array('logged_status' => $logged_status));

JS:

if(my_plugin_vars.logged_status == 'loggedin') {
    console.log("user is logged in");
} else {
    console.log("user is logged out");
}
Sign up to request clarification or add additional context in comments.

7 Comments

this did work but I am getting a "GET xxxx.com/wp-content/plugins/home/dmipar5/public_html/xxxx.com/… 404 (Not Found)" in my console (I xxxx'ed out my name). Am I supposed to fill in something for 'my-script-handle' or 'my_plugin_vars;?
It looks like dmi.js is not enqueueing properly. Is dmi.js the javascript file that you're using for the above issue?
yes it's my javascript file that I have the JS you suggested in.
Looks like your plugin is in /wp-content/plugins/home, correct? If that's the case, the wp_enqueue_script line above should be: wp_enqueue_script('my-script-handle', plugins_url( '/js/dmi.js', __FILE__), array('jquery'));
no the path to my plugin is /wp-content/plugins/front-end-only-users, I'm not sure where the home is coming from
|
0

If you are generating all the code from your PHP script i.e. the variable assignment AND the actual javascript code, this should work.

<?php
function loggedin() {
    $FEUP = new FEUP_User;
    if ($FEUP->Is_Logged_In()) {
        return 'true';
    } else {
        return 'false';
    }
}

echo '<script type="text/javascript">';
echo 'var user = <?php echo loggedin(); ?>;';
echo 'if(user) {
        console.log("user is logged in");
      }else {
        console.log("user is logged out");
      }';
echo '</script>';
?>

Or if your javascript is kept seperately then all you need to do is output the javascript that sets the variable

<?php
function loggedin() {
    $FEUP = new FEUP_User;
    if ($FEUP->Is_Logged_In()) {
        return 'true';
    } else {
        return 'false';
    }
}

echo '<script type="text/javascript">';
echo 'var user = <?php echo loggedin(); ?>;';
echo '</script>';

But your javascript that test this variable must not run until the variable assignment piece of javascript has executed and loaded that variable into the Global namespace.

From your code example, its not easy to see exactly which of these possibilities bets fits your actual situation.

1 Comment

I tried both and am still getting the same results, If I do the top example I always get "user logged in" and with the bottom example I always get "user logged out". I placed the php in the header.php file in my wordpress theme.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.