wordpress 插件 ->调用未定义的函数 wp_get_current_user() [英] wordpress plugin -> Call to undefined function wp_get_current_user()

查看:24
本文介绍了wordpress 插件 ->调用未定义的函数 wp_get_current_user()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 func wp_get_current_user() 在我的插件中获取当前用户信息.但我越来越调用未定义的函数 wp_get_current_user()

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()

显然这是因为包含该函数的文件 /wp-includes/pluggable 直到插件加载后才会加载.

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?

推荐答案

显然这是因为包含该函数的文件/wp-includes/pluggable 在加载插件之后才会加载.

Apparently this is happening because the file /wp-includes/pluggable which contains the function doesn't get loaded until after the plugins are loaded.

确实如此.因此,将您正在做的任何事情包装在一个函数中,并将其挂接到 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)

示例:

add_action('init','do_stuff');
function do_stuff(){
  $current_user = wp_get_current_user();
  // ...
}

这篇关于wordpress 插件 ->调用未定义的函数 wp_get_current_user()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆