获取在jquery中使用的php函数信息

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

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");
}

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.