I am trying to use WordPress authentication on a custom PHP page however when checking "is_user_logged_in" it returns nothing at all and other functions are also returning nothing helpful.

The code I am using is:

require $_SERVER['DOCUMENT_ROOT'] . "/wp-load.php";
require $_SERVER['DOCUMENT_ROOT'] . "/wp-blog-header.php";

I have logged into the admin area prior to testing this. When being logged in or logged out the function still doesn't return anything and there are no PHP errors.


Is my global set up?

As you can see from taking a look at the »Help«-panel, when you're using my »Current Admin Screen Info« 1), there're certain points/hooks/filters in code, that need to pass by before a global is set up.

enter image description here

1) Still searching for a better name.

When to hook?

Yesterday, while reading WPSE chat, I had a nice idea that was the base for above mentioned plugin. To give you an idea of how to check if a global is set up, simply read the following code:

function get_hook_for_global()
     $global = $GLOBALS['some_global'];
     if ( isset( $global ) )
          remove_filter( current_filter(), __FUNCTION__ );
          return print "Global: $global<br />Set on: ".current_filter();
add_action( 'all', 'get_hook_for_global' );

This prints out the contents of the global, as well as the filter where it was set.

Improving the check

If you want to have detailed output, you'll have to add array/object handling as well:

// Object/Array handling: Dump contents
if ( is_array( $global ) OR is_object( $global ) )
    $global = var_export( $global, true );
  • I am trying to use my code completely outside of WordPress and only want the login authentication part of WordPress so how does something like this help me exactly? Sorry, I am a tad confused. – Rambomst Sep 14 '12 at 2:29
  • @Rambomst Just loading parts of WP doesn't help much. You then still have to use the right places/hooks/filters where the global is ready. If it completely isn't (or you don't have the filter system), then your problem is that you loaded the WP ecosystem incompletely. But there're other answers on the site that tell you how to hook the relevant parts. – kaiser Sep 14 '12 at 2:33

You can't load WordPress like that and have the authentication mechanisms work. The auth cookies are bound to the site URL and only sent to those URLs. If your URL scheme is outside the normal WordPress URL scheme, then it won't get the cookies, and you won't be logged into WordPress when accessing it through that URL.

In other words, you cannot check authentication in the way you're attempting to check it. Just including wp-load.php isn't enough to authenticate you.

  • Okay, thanks for the reply, do you know of a way to do it? Any help is much appreciated. – Rambomst Sep 14 '12 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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