wordpress plugin -> Call to undefined function wp_

2019-01-18 02:18发布

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?

9条回答
等我变得足够好
2楼-- · 2019-01-18 03:02

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();
  // ...
}
查看更多
地球回转人心会变
3楼-- · 2019-01-18 03:02

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');
查看更多
再贱就再见
4楼-- · 2019-01-18 03:03

Quick fix include_once(ABSPATH . 'wp-includes/pluggable.php'); add this line on your capabilities.php

查看更多
登录 后发表回答