简体   繁体   English

如何在非wp页面中查找当前wordpress用户的名称

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

I link from WP to a non-wp page. 我从WP链接到非wp页面。 How can I find the username of the person that logged into wp? 如何找到登录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(). 文档指出它是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. 如果由于您不在非wp页面上而无法正常工作。 Try replacing wp_get_current_user() with get_currentuserinfo(). 尝试用get_currentuserinfo()替换wp_get_current_user()。

Your non-wp page is not in WP folder, so it won't have access to WP cookie. 您的非wp页面不在WP文件夹中,因此它无法访问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. 如果您想通过位于WP文件夹之外的php脚本看到WP cookie,则需要定义COOKIEPATH以指向php脚本和wp文件夹的公共父文件夹,或者仅将其指向根文件夹。 Put the following line in your wp-config.php: 将以下行放在wp-config.php中:

define('COOKIEPATH', '/');

PS: Also your non-wp page should be served from the same domain as WP. PS:同样,您的非wp页面也应与WP来自同一域。

This is working well. 这运作良好。

In Wordpress I saved a cookie. 在Wordpress中,我保存了一个cookie。 Then in a non-wordpress page, before the DOCTYPE line, I placed this code: 然后在非WordPress页面中,在DOCTYPE行之前,放置以下代码:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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