wordpress插件->调用未定义的函数wp_get_current_user

问题描述

| 我正在尝试使用func wp_get_current_user()获取插件中的当前用户信息。但是越来越
Call to undefined function wp_get_current_user()
显然发生这种情况是因为包含该函数的文件ѭ1until直到加载插件后才被加载。 有人对如何在我的插件中获取用户详细信息有任何想法吗?     

解决方法

  显然是由于包含插件的功能的文件/ wp-includes / pluggable直到加载插件后才加载而发生的。 它的确是。因此,将您正在执行的任何操作都包装在函数中,然后将其挂钩到plugins_loaded或init挂钩上。 (请参阅wp-settings.php文件) 例:
add_action(\'init\',\'do_stuff\');
function do_stuff(){
  $current_user = wp_get_current_user();
  // ...
}
    ,你可以用这个
<?php
if(!function_exists(\'wp_get_current_user\')) {
    include(ABSPATH . \"wp-includes/pluggable.php\"); 
}
?>
这应该可以解决您的问题:)     ,尝试也添加
require_once(\'../../../wp-load.php\');
随着
require_once(ABSPATH.\'wp-includes/pluggable.php\');
    ,安装wp 3.8之后,我在用ajax获得的页面上也遇到了同样的问题。我用以下代码修复了它:
if(!function_exists(\'wp_delete_user\')) {
    include(ABSPATH . \"wp-admin/includes/user.php.\");
}
显然,该功能已从pluggable.php移至user.php。在包含wp-blog-header.php之后,我仍然不明白为什么它不起作用。     ,我的问题用此代码解决了
include_once(ABSPATH . \'wp-includes/pluggable.php\');
    ,更新WP后,我得到了相同的错误消息。对我有用的修补程序很容易: 在wp-includes目录(WP 3.8.x)中找到capability.php。 在php标签之后的顶部添加以下内容:
require_once(\'pluggable.php\');
    ,不是
wp-includes
,但:
include_once(ABSPATH . \"wp-admin/includes/plugin.php\");
    ,听起来可能很疯狂,但由于我有一个名为menu.php的文件,所以在我的应用程序中出现了问题,我在其中有一个创建Wordpress菜单的类。 从字面上看,只需将FILE的名称从menu.php更改为nav-menu.php即可解决此问题。我重复了3次此问题,因为我不敢相信FILE的名称可能是问题所在。 以防万一有人想要现在该文件中的内容,这里是:
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>\';
        }        
    }
}
    ,快速修复
include_once(ABSPATH . \'wp-includes/pluggable.php\');
在您的capabilities.php中添加此行     

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...