WordPress 插件 ->调用未定义函数 wp_get_current_user()
我正在尝试使用 func wp_get_current_user() 获取插件中的当前用户信息。但我越来越 调用未定义的函数 wp_get_current_user()
显然,发生这种情况是因为包含该函数的文件 /wp-includes/pluggable
直到插件加载后才加载。
有人对如何在我的插件中获取用户详细信息有任何想法吗?
I'm trying to get the current user info in my plugin using the func wp_get_current_user(). But am gettingCall to undefined function wp_get_current_user()
Apparently this is happening because the file /wp-includes/pluggable
which contains the function doesn't get loaded until after the plugins are loaded.
Anybody any ideas on how to get the user details in my plugin?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
确实如此。因此,将您正在做的任何事情包装在函数中,并将其挂接到plugins_loaded 或init 挂钩上。 (请参阅 wp-settings.php 文件)
示例:
Indeed it is. So wrap whichever thing you're doing in a function, and hook it onto the plugins_loaded or init hook. (see the wp-settings.php file)
Example:
你可以使用这个,
这应该可以解决你的问题:)
You can use this,
this should fix your problem :)
我的问题用这段代码解决了
my issue solved with this code please
尝试同时
添加
try adding also
along with
安装 wp 3.8 后,我在使用 ajax 获得的页面上遇到了同样的问题。我用以下代码修复了它:
显然该函数已从pluggable.php移至user.php。我仍然不明白为什么在包含 wp-blog-header.php 后它不起作用。
After the installation of wp 3.8 I had the same problem with a page I get with ajax. I fixed it with the following code:
Apparently the function is moved from pluggable.php to user.php. Still I don't understand why it doesn't work after I included the wp-blog-header.php.
尽管这听起来很疯狂,但我的应用程序中出现了问题,因为我有一个名为 menu.php 的文件,其中有一个类来创建 Wordpress 菜单。
从字面上看,只需将文件名从 menu.php 更改为 nav-menu.php 即可解决问题。我将这个问题复制了 3 次,因为我不敢相信文件名可能是问题所在。
以防万一有人想知道该文件中的内容,这里是:
As crazy as this might sound, the problem in my application was happening because I had a FILE called menu.php where I had a class to create Wordpress menus.
Literally, simply changing the name of the FILE from menu.php to nav-menu.php, fixed the issue. I replicated the issue 3 times because I could not believe the name of the FILE could be the problem.
Just in case somebody would like to now what was inside that file, here it is:
尝试在插件 PHP 文件中使用它。
try using it in the plugin PHP file.
我也遇到过这种情况,因为我在通过 CLI 安装 WordPress 后没有等待足够的时间才打开网站
It also happened to me just because I had not waited enough after Wordpress installation by CLI before opening the website
更新 WP 后,我收到了同样的错误消息。对我有用的修复方法既快速又简单:
在 wp-includes 目录 (WP 3.8.x) 中找到 features.php。
在顶部的 php 起始标记之后添加以下内容:
I got the same error message after updating WP. The fix that worked for me is quick and easy:
Locate capabilities.php in the wp-includes directory (WP 3.8.x).
Add the following at the top, after the opening php tag:
不是
wp-includes
而是:NOT
wp-includes
but :如果您过早调用 add_menu_page 或 add_submenu_page ,也可能会发生此错误。它应该在加载管理菜单后完成,如下所述:
https://stackoverflow.com/a/24669032/612253
This error can also occur if you call add_menu_page or add_submenu_page too early. It should be done after the admin menu is loaded, as described here:
https://stackoverflow.com/a/24669032/612253
快速修复
include_once(ABSPATH . 'wp-includes/pluggable.php');
将这一行添加到您的 features.php 中Quick fix
include_once(ABSPATH . 'wp-includes/pluggable.php');
add this line on your capabilities.php