Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

wordpress plugin -> Call to undefined function wp_get_current_user()

Tags:

php

wordpress

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?

like image 729
Daithí Avatar asked May 25 '11 16:05

Daithí


1 Answers

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();   // ... } 
like image 55
Denis de Bernardy Avatar answered Sep 22 '22 21:09

Denis de Bernardy