简体   繁体   中英

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/>';

?>

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().

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.

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";  }
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM