简体   繁体   English

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

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

I have the following issue. 我有以下问题。 I want to load the Wordpress environment into an external php file. 我想将Wordpress环境加载到外部php文件中。 All my work is currently on a localhost and my php code is as follows: 我所有的工作当前都在本地主机上,而我的php代码如下:

<?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 . 这在我的本地主机上工作得很好,所以我认为将文件上传到服务器时它也必须工作,因为文件与本地存储在同一目录中,因此../../wp-blog-header.php Sadly, this does not work. 可悲的是,这不起作用。 When I echo $admin; 当我echo $admin; the following error appears: 出现以下错误:

User: user ID is incorrect 用户:用户标识不正确

I tried using the native Wordpress functions without using the shortcode like this: 我尝试使用本机Wordpress函数而不使用这样的短代码:

<?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? 有没有更有效的方式将Wordpress环境加载到外部php文件中? 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. 它不一定是电子邮件,也可以是用户ID,我尝试过并给我0。

I forgot to mention, the php file loads and echoes Forbidden together with the error. 我忘了提,php文件加载并与错误一起回显“ 禁止 ”。 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. 这使我相信,实际上正在加载文件require("../../wp-blog-header.php")因为当我更改目录时,我直接得到一个错误,表明该文件现在可以工作/可用。

The shortcode works as I have tested it inside a new Page returning the e-mail address of the logged-in user. 简码可以正常工作,因为我已经在新的Page中对其进行了测试,返回了已登录用户的电子邮件地址。


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. 我也用require("/var/www/vhosts/test-website.com/httpdocs/wp-blog-header.php")进行了测试。 Still same result. 结果还是一样。 The weird thing is that if I add wp_head() below this line, the header gets loaded. 奇怪的是,如果我在此行下面添加wp_head() ,则会加载标头。 I reformulate my question: 我重新提出我的问题:

How can I get the current e-mail address of a logged-in user in an external php file? 如何在外部php文件中获取登录用户的当前电子邮件地址?

If your file is same folder root with Wordpress instance. 如果您的文件与Wordpress实例的文件夹根目录相同。 just use code bellow: 只需使用以下代码:

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

//your stuff //你的东西

暂无
暂无

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

相关问题 如何使用WordPress SHORTINIT获取当前的登录用户 - How Can I Get the Current Logged-In User with WordPress SHORTINIT Laravel:如何通过电子邮件对用户进行身份验证,其中电子邮件地址存储在单独的表中? - Laravel: How can I authenticate a user by e-mail, where the e-mail address is stored in a separate table? 如何检查用户是否已登录 php? - How can I check if a user is logged-in in php? 使用外部php脚本获取joomla当前登录用户的用户名 - Get username of current logged-in user of joomla using external php script 如何将“发件人”电子邮件地址添加到PHP表单? - How can I add a From e-mail address to a PHP form? 如何从Wordpress中的新用户欢迎电子邮件中的密码链接中删除&lt;&gt;字符? - How can I remove the <> characters from the password link in the new user welcome e-mail in Wordpress? 如何将 php 文件的访问限制为每天一次? (具体来说,php 文件发送一封电子邮件,我希望每个用户/IP 每天一封电子邮件) - How to restrict access to a php file to once per day? (specifically, the php file sends an e-mail, I want one e-mail per day per user/IP) 如何使用materializeCSS和PHP发送电子邮件 - How can I send an E-mail using materializeCSS and PHP 如何使用PHP将主机example.com与诸如user@example.com之类的电子邮件地址分开? - How I separate the host example.com from an e-mail address like user@example.com using PHP? 如何从 Fluid 获取当前登录的用户? - How to get the current logged-in user from Fluid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM