WordPress 插件 ->调用未定义函数 wp_get_current_user()

发布于 2024-11-09 09:43:49 字数 217 浏览 0 评论 0原文

我正在尝试使用 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 getting
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.

Anybody any ideas on how to get the user details in my plugin?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(12

夏见 2024-11-16 09:43:49

显然发生这种情况是因为包含该函数的文件 /wp-includes/pluggable 在加载插件之后才会加载。

确实如此。因此,将您正在做的任何事情包装在函数中,并将其挂接到plugins_loaded 或init 挂钩上。 (请参阅 wp-settings.php 文件)

示例:

add_action('init','do_stuff');
function do_stuff(){
  $current_user = 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.

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:

add_action('init','do_stuff');
function do_stuff(){
  $current_user = wp_get_current_user();
  // ...
}
嗼ふ静 2024-11-16 09:43:49

你可以使用这个,

<?php
if(!function_exists('wp_get_current_user')) {
    include(ABSPATH . "wp-includes/pluggable.php"); 
}
?>

这应该可以解决你的问题:)

You can use this,

<?php
if(!function_exists('wp_get_current_user')) {
    include(ABSPATH . "wp-includes/pluggable.php"); 
}
?>

this should fix your problem :)

梦魇绽荼蘼 2024-11-16 09:43:49

我的问题用这段代码解决了

include_once(ABSPATH . 'wp-includes/pluggable.php');

my issue solved with this code please

include_once(ABSPATH . 'wp-includes/pluggable.php');
凡间太子 2024-11-16 09:43:49

尝试同时

require_once('../../../wp-load.php');

添加

require_once(ABSPATH.'wp-includes/pluggable.php');

try adding also

require_once('../../../wp-load.php');

along with

require_once(ABSPATH.'wp-includes/pluggable.php');
夏の忆 2024-11-16 09:43:49

安装 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 后它不起作用。

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:

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.

转身以后 2024-11-16 09:43:49

尽管这听起来很疯狂,但我的应用程序中出现了问题,因为我有一个名为 menu.php 的文件,其中有一个类来创建 Wordpress 菜单。

从字面上看,只需将文件名从 menu.php 更改为 nav-menu.php 即可解决问题。我将这个问题复制了 3 次,因为我不敢相信文件名可能是问题所在。

以防万一有人想知道该文件中的内容,这里是:

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>';
        }        
    }
}

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:

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>';
        }        
    }
}
陌上青苔 2024-11-16 09:43:49

尝试在插件 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 );

try using it in the plugin PHP file.

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 );
小鸟爱天空丶 2024-11-16 09:43:49

我也遇到过这种情况,因为我在通过 CLI 安装 WordPress 后没有等待足够的时间才打开网站

It also happened to me just because I had not waited enough after Wordpress installation by CLI before opening the website

懒猫 2024-11-16 09:43:49

更新 WP 后,我收到了同样的错误消息。对我有用的修复方法既快速又简单:

在 wp-includes 目录 (WP 3.8.x) 中找到 features.php。
在顶部的 php 起始标记之后添加以下内容:

require_once('pluggable.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:

require_once('pluggable.php');
记忆里有你的影子 2024-11-16 09:43:49

不是 wp-includes 而是:

include_once(ABSPATH . "wp-admin/includes/plugin.php");

NOT wp-includes but :

include_once(ABSPATH . "wp-admin/includes/plugin.php");
紫瑟鸿黎 2024-11-16 09:43:49

如果您过早调用 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

萝莉病 2024-11-16 09:43:49

快速修复 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文