Load wordpress from local context of external application (from within a function)

If I want to use wordpress functions from an external app, I’d do as follows:

require_once('/path/to/wordpress/wp-load.php');
$isLoggedIn = is_user_logged_in();

This works without problems. But I would like to load wordpress from within a function. Something like this:

Read More
function wpIsUserLoggedIn()
{
    global $wpdb; // Otherwise there are bunch of errors when including wp-load.php
    require_once('/path/to/wordpress/wp-load.php');
    return is_user_logged_in(); // Always returns false
}

In this case is_user_logged_in() always returns false. What am I missing? Is there a way to load WP from within a function?

Related posts