[英]wordpress plugin -> Call to undefined function wp_get_current_user()
I'm trying to get the current user info in my plugin using the func wp_get_current_user().我正在尝试使用 func wp_get_current_user() 在我的插件中获取当前用户信息。 But am getting
Call to undefined function 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.显然,这是因为包含 function 的文件
/wp-includes/pluggable
直到加载插件后才会加载。
Anybody any ideas on how to get the user details in my plugin?有人对如何在我的插件中获取用户详细信息有任何想法吗?
Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.
显然,这是因为包含 function 的文件 /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.
因此,将您正在做的任何事情包装在 function 中,并将其挂接到 plugins_loaded 或 init 钩子上。 (see the wp-settings.php file)
(参见 wp-settings.php 文件)
Example:例子:
add_action('init','do_stuff');
function do_stuff(){
$current_user = wp_get_current_user();
// ...
}
You can use this,你可以用这个,
<?php
if(!function_exists('wp_get_current_user')) {
include(ABSPATH . "wp-includes/pluggable.php");
}
?>
this should fix your problem:)这应该可以解决您的问题:)
After the installation of wp 3.8 I had the same problem with a page I get with ajax.安装 wp 3.8 后,我在使用 ajax 获得的页面时遇到了同样的问题。 I fixed it with the following code:
我用以下代码修复了它:
if(!function_exists('wp_delete_user')) {
include(ABSPATH . "wp-admin/includes/user.php.");
}
Apparently the function is moved from pluggable.php to user.php.显然 function 已从 pluggable.php 移至 user.php。 Still I don't understand why it doesn't work after I included the wp-blog-header.php.
我仍然不明白为什么在包含 wp-blog-header.php 后它不起作用。
try adding also也尝试添加
require_once('../../../wp-load.php');
along with随着
require_once(ABSPATH.'wp-includes/pluggable.php');
my issue solved with this code please请用这段代码解决我的问题
include_once(ABSPATH . 'wp-includes/pluggable.php');
I got the same error message after updating WP.更新 WP 后,我收到了相同的错误消息。 The fix that worked for me is quick and easy:
对我有用的修复方法既快速又简单:
Locate capabilities.php in the wp-includes directory (WP 3.8.x).在 wp-includes 目录 (WP 3.8.x) 中找到 capabilities.php。 Add the following at the top, after the opening php tag:
在顶部,在打开 php 标签之后添加以下内容:
require_once('pluggable.php');
wp-includes
but:wp-includes
但:include_once(ABSPATH . "wp-admin/includes/plugin.php");
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.尽管这听起来很疯狂,但我的应用程序中的问题正在发生,因为我有一个名为menu.php的文件,其中我有一个 class 来创建 Wordpress 菜单。
Literally, simply changing the name of the FILE from menu.php to nav-menu.php , fixed the issue.从字面上看,只需将文件的名称从menu.php 更改为nav-menu.php 即可解决问题。 I replicated the issue 3 times because I could not believe the name of the FILE could be the problem.
我将问题复制了 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>';
}
}
}
try using it in the plugin PHP file.尝试在插件 PHP 文件中使用它。
if ( !function_exists('wp_get_current_user') ) {
include(ABSPATH . "wp-includes/pluggable.php");
}
$current_user = wp_get_current_user();
$user=esc_html( $current_user->ID );
It also happened to me just because I had not waited enough after Wordpress installation by CLI before opening the website我也遇到过这种情况,只是因为我在打开网站之前通过 CLI 安装 Wordpress 后没有等待足够的时间
Quick fix include_once(ABSPATH. 'wp-includes/pluggable.php');
快速修复
include_once(ABSPATH. 'wp-includes/pluggable.php');
add this line on your capabilities.php在你的能力上添加这一行。php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.