how to find name of current wordpress user in non-wp page

I link from WP to a non-wp page. How can I find the username of the person that logged into wp? This doesn’t work.

  <?php 
    require_once("../members/wordpress/wp-load.php");


if(is_user_logged_in())
{
    echo '<br />User Logged in ok<br />';
    echo 'User ID is: '.$user_ID.'<br />';
    echo 'User login is: '.$current_user->user_login.'<br />';
}
else
    echo 'No user is logged in<br/>';

?>

Related posts

Leave a Reply

3 comments

  1. Try

    wp_get_current_user();
    

    Documentation states that it is a wrapper of get_currentuserinfo().

    So, try this;

    <?php
        $current_user = wp_get_current_user();
        if ( 0 == $current_user->ID ) {
            // Not logged in.
        } else {
            // Logged in.
        }
    ?>
    

    If it does not work since you are on a non-wp page. Try replacing wp_get_current_user() with get_currentuserinfo().

  2. Your non-wp page is not in WP folder, so it won’t have access to WP cookie. If you want WP cookie to be seen by php script located outside of your WP folder, need to define COOKIEPATH to point to common parent folder of your php script & your wp folder, or just point it to root folder.
    Put the following line in your wp-config.php:

    define('COOKIEPATH', '/');
    

    PS: Also your non-wp page should be served from the same domain as WP.

  3. This is working well.

    In WordPress I saved a cookie. Then in a non-wordpress page, before the
    DOCTYPE line, I placed this code:

    <?php
        session_start();   
        $name =  $_SESSION['member_name'];
        if ($name == '') {
          header('Location: searchObits.php'); 
          exit;
        } else {  $_SESSION["ismember"] = "member";  }
    ?>