php wordpress 插件 -> 调用未定义的函数 wp_get_current_user()
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6127559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
wordpress plugin -> Call to undefined function wp_get_current_user()
提问by Daithí
I'm trying to get the current user info in my plugin using the func wp_get_current_user(). But am getting
Call to undefined function wp_get_current_user()
我正在尝试使用 func wp_get_current_user() 在我的插件中获取当前用户信息。但我越来越
Call 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.
显然这是因为在/wp-includes/pluggable
加载插件之后才加载包含该函数的文件。
Anybody any ideas on how to get the user details in my plugin?
有人对如何在我的插件中获取用户详细信息有任何想法吗?
回答by Denis de Bernardy
Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.
显然这是因为包含该函数的文件 /wp-includes/pluggable 在加载插件之后才会加载。
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)
的确是。因此,将您正在做的任何事情包装在一个函数中,并将其挂接到 plugins_loaded 或 init 钩子上。(见 wp-settings.php 文件)
Example:
例子:
add_action('init','do_stuff');
function do_stuff(){
$current_user = wp_get_current_user();
// ...
}
回答by Daithí
You can use this,
你可以用这个,
<?php
if(!function_exists('wp_get_current_user')) {
include(ABSPATH . "wp-includes/pluggable.php");
}
?>
this should fix your problem :)
这应该可以解决您的问题:)
回答by Mau
try adding also
也尝试添加
require_once('../../../wp-load.php');
along with
随着
require_once(ABSPATH.'wp-includes/pluggable.php');
回答by Benjamin Ziepert
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:
安装 wp 3.8 后,我在使用 ajax 获得的页面上遇到了同样的问题。我用以下代码修复了它:
if(!function_exists('wp_delete_user')) {
include(ABSPATH . "wp-admin/includes/user.php.");
}
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.
显然,该功能已从 pluggable.php 移动到 user.php。我仍然不明白为什么在我包含 wp-blog-header.php 后它不起作用。
回答by Kapil Goyal
my issue solved with this code please
请用此代码解决我的问题
include_once(ABSPATH . 'wp-includes/pluggable.php');
回答by T.Todua
NOT wp-includes
but :
不但是wp-includes
:
include_once(ABSPATH . "wp-admin/includes/plugin.php");
回答by drjorgepolanco
As crazy as this might sound, the problem in my application was happening because I had a FILE called menu.phpwhere I had a class to create Wordpress menus.
尽管这听起来很疯狂,但我的应用程序中的问题发生了,因为我有一个名为menu.php的文件,其中有一个类来创建 Wordpress 菜单。
Literally, simply changing the name of the FILE from menu.phpto 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.
从字面上看,只需将 FILE 的名称从menu.php 更改为nav-menu.php 即可解决问题。我将这个问题复制了 3 次,因为我无法相信 FILE 的名称可能是问题所在。
Just in case somebody would like to now what was inside that file, here it is:
以防万一有人想知道该文件中的内容,这里是:
class OwNavMenu extends OwCpnt
{
function __construct( $location, $args ) {
$show = $args['show'];
$span = $args['span'];
if ( $show ) {
$this->menu( $location, $span );
}
}
function menu( $location, $span ) {
if ( $location ) {
echo '<div id="ow-' . $location . '" class="ow-nav ow-' . $location . '">';
wp_nav_menu(
array(
'theme_location' => $location,
'link_before' => ( $span ) ? '<span>' : '',
'link_after' => ( $span ) ? '</span>' : ''
)
);
echo '</div>';
}
}
}
回答by colind
I got the same error message after updating WP. The fix that worked for me is quick and easy:
更新 WP 后,我收到相同的错误消息。对我有用的修复既快速又简单:
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 目录 (WP 3.8.x) 中找到 capabilities.php。在顶部添加以下内容,在打开的 php 标签之后:
require_once('pluggable.php');
回答by Mikhail Villamor
Quick fix include_once(ABSPATH . 'wp-includes/pluggable.php');
add this line on your capabilities.php
快速修复include_once(ABSPATH . 'wp-includes/pluggable.php');
在你的 capabilities.php 上添加这一行