Support » Fixing WordPress » Authenticate WordPress login from External PHP script?

  • Hi,

    I’m not sure WHY I cannot find this information. Basically I need to run several external PHP scripts on the same domain as my WordPress blog. What I want to do is authenticate that the person running the external PHP script is currently logged in to WordPress (at any level, I don’t care which role they have).

    I don’t need anything fancy, or even very secure, because I know my users are not tech savvy enough to hack through it. All I need is something simple that will look to see – is this person currently logged in to my WordPress blog right now. Here is what I had, it did work for a few days, now it does not work??
    ini_set(‘display_errors’, 1);
    require_once(‘../../../wp-config.php’); //Please make sure that the path is correct

    global $user_ID;

    $wp_user = new WP_User($user_ID);
    if (true == empty($wp_user) || $wp_user->ID < 1) {
    //In this case we are dealing with a visitor, not member
    wp_die(“Would you please be so kind to log in?”);

    Any ideas on why this used to work, and now it does not, or why it cannot work now? Any other ways I can do this? Every time I run it now I end up with $wp_user=NOT EMPTY, and ID=0, regardless of whether I am logged in to my WordPress blog, or not? The only thing different, I changed themes, I did try changing back to the new theme, it did not fix the problem, so that might not be what caused this.

    Any help greatly appreciated, I don’t know why this is so difficult, seems it would be a fairly straight-forward way to do it?


Viewing 5 replies - 1 through 5 (of 5 total)
  • Did you found any info on that?


    I think I’d check for the presence of WP’s login cookies.


    I actually found the code on another blog, here it is:

    $user_login = 'admin';
    $user = get_userdatabylogin($user_login);
    $user_id = $user->ID;
    wp_set_current_user($user_id, $user_login);
    do_action('wp_login', $user_login);

    remember to change the location of the wp-blog-header.php file as well as the user_login variable.

    the code was found here:



    lkoudal, that will set the user to your admin user, no matter who the user really is. Be careful.

    Maybe that’s what you want to do.



    I did the following:

    Add the following in your wp-config.php:

    define('COOKIE_DOMAIN', '');
    define('COOKIEPATH', '/');

    You can make the path more specific (e.g. if your external script is in /myscript/ and your WP installation is in /myscript/wordpress/, then you can use ‘/myscript/’ as COOKIEPATH).

    And then in my external script:

    if (! is_user_logged_in()) {
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Authenticate WordPress login from External PHP script?’ is closed to new replies.