[英]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.