duansha7025 2016-05-11 21:46
浏览 42

如何在外部php文件中获取已登录用户的当前Wordpress电子邮件地址?

I have the following issue. I want to load the Wordpress environment into an external php file. All my work is currently on a localhost and my php code is as follows:

<?php
require("../../wp-blog-header.php");

$admin = do_shortcode("[su_user field='user_email']");

if ($admin === "admin@email.com") {

    // Do stuff here

}

else {

    echo "<h1>Forbidden.</h1>";

}

This works perfectly fine in my localhost, so I figured that when I upload the file to the server it must work too, given that the file is stored in the same directory as it is locally, hence ../../wp-blog-header.php. Sadly, this does not work. When I echo $admin; the following error appears:

User: user ID is incorrect

I tried using the native Wordpress functions without using the shortcode like this:

<?php global $user_email;
      get_currentuserinfo();

      echo $user_email;
?>

This does not work either. Is there a more efficient way to load the Wordpress environment into an external php file? Any suggestions on what is happening here? It does not necessarily have to be an e-mail, it could be the user id as well, which I tried and gives me 0.

I forgot to mention, the php file loads and echoes Forbidden together with the error. This makes me believe that the file require("../../wp-blog-header.php") is actually being loaded because when I change the directory I directly get an error that the file is now working/available.

The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user.


UPDATE

My root is:

/var/www/vhosts/test-website.com/httpdocs/

Which I tested with require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php") as well. Still same result. The weird thing is that if I add wp_head() below this line, the header gets loaded. I reformulate my question:

How can I get the current e-mail address of a logged-in user in an external php file?

  • 写回答

1条回答 默认 最新

  • dongzhiyan5693 2016-05-12 07:17
    关注

    If your file is same folder root with Wordpress instance. just use code bellow:

    <?php
    define( 'ROOT_PATH', dirname(__FILE__) );
    require(ROOT_PATH . "/wp-blog-header.php");
    

    //your stuff

    评论

报告相同问题?

悬赏问题

  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法