Not quite sure why this is happening, hopefully someone can help. I have a wordpress installation, lets say for arguments sake it's installed to http://www.example.com/wordpress. I have a file called test.php with the following code:

    include '/php_path_to_blog/wp-blog-header.php';
    echo get_current_user_id();

Now the odd part is if i have a user logged in and place this file within my wordpress directory and call it by going to http://www.example.com/wordpress/test.php it outputs the correct user id of the current logged in user. However if I move the file to the root directory of the site and call it by going to http://www.example.com/test.php, it outputs 0 as if there were no user logged in.

It seems that some functions work properly outside of the installation directory and some don't. I would like them to work outside the wordpress isntallation directory.

Any help would be greatly appreciated.

  • 2
    One of my favorite code snippets when I have no idea where the issue might lie is this: <pre><?php print_r(get_defined_vars)); ?></pre> It will show you every variable currently available in a nicely formatted display. You might try putting this under your include statement to see what (if anything) is being called and how to access it.
    – SickHippie
    Commented Mar 9, 2012 at 20:34
  • that really cool @SickHippie didnt know this command (spelling wrong there.. its <pre><?php print_r(get_defined_vars()); ?></pre>... thanks a lot man
    – Sagive
    Commented Mar 9, 2012 at 21:02
  • Whoops! Good catch - it won't work very well without (). That function has saved me hours of trouble, if not days.
    – SickHippie
    Commented Mar 9, 2012 at 21:08

1 Answer 1


Very similar to this question of a couple of hours ago: How can I get a list of latest posts outside of my WP install?

Use chdir() to move into WordPress root before including and even calling anything related to WordPress. The are cases where relative directory and file references are used, thus breaking many things.

You can return to the previous directory afterwards by storing the get_cwd() first.

$cwd = get_cwd();
chdir( 'wordpress_root' );
include 'wp-load.php';
// ... do stuff with WordPress
chdir( $cwd );
// ... continue happily ever after

Additionally, seeing that you're working with session cookies, the paths for these cookies are invalid for the URI you're working from. The browser will not send the WordPress authentication cookies that you have acquired previously since the domain roots are invalid and outside of the WordPress scope. You will need to alter the roots of these cookies from WordPress with something like this http://wordpress.org/extend/plugins/root-cookie/.

  • Doesn't work for me. Can you help me understand better? Commented Mar 10, 2012 at 8:57
  • Can you paste the code that doesn't work for you? Are you chdir()ing into the /php_path_to_blog/?
    – soulseekah
    Commented Mar 10, 2012 at 12:24
  • chdir ($WP_relpath); require_once('./wp-load.php'); require_once('./wp-includes/registration.php'); $wpuser=wp_get_current_user();. I am doing this inside a mediawiki UserLoadFromSession hook, if that helps. I also checked that the directory is successfully changed using getcwd(); after I chdir()ed. Commented Mar 10, 2012 at 12:29
  • I think the problem also lies in the fact that cookies are tied to the $WP_relpath URI and cannot be sent by the browser. Updated my answer, see if this helps.
    – soulseekah
    Commented Mar 10, 2012 at 16:55
  • 1
    Does this still work for other people? Started breaking for me from the last wordpress update a day or 2 ago.
    – bmiskie
    Commented Feb 8, 2018 at 21:33

Your Answer

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

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