46

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?

12 Answers 12

73

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();
  // ...
}
4
  • 3
    worked like a charm. I was working of a class that loaded the user details on construct. So here's the code for anyone else. I now have: `add_action('plugins_loaded','construct_my_class'); function construct_my_class(){ $controller = new myClass(); }
    – Daithí
    Commented May 25, 2011 at 19:28
  • @Prakash depends on your plugin architecture. For most wordpress development I would imagine it would go in the plugins index.php file
    – Daithí
    Commented Mar 18, 2014 at 21:21
  • @denis de Bernardy - Now do I use this? I'm trying to "limit" add_meta_box to admin only ... include(ABSPATH . "wp-includes/pluggable.php") worked for me, but you say it will break WP ...
    – Levchik
    Commented Dec 4, 2017 at 17:25
  • What if you get undefined function for add_action? Commented Jul 18, 2022 at 13:15
26

You can use this,

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

this should fix your problem :)

3
  • @DenisdeBernardy I want to implement this solution but I want to know, how will this break WP?
    – user2806040
    Commented Feb 4, 2017 at 11:20
  • 1
    @Lumo5: I haven't touched WP for years, but off the top of my head a) is the file always included using include_once by WP? b) might you not end up opening security issues in adding the pluggable functions (in particular the ones related to authentication and permission management before proper authentication takes place)? and c) there's a hook for what OP wanted to do. Commented Feb 17, 2017 at 18:56
  • @DenisdeBernardy which hook would be better to use instead?
    – Levchik
    Commented Dec 4, 2017 at 17:16
6

my issue solved with this code please

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

try adding also

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

along with

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

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.

1

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

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 );
1

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

1
  • Yes, it's exactly happens only if Wordpress hasn't been initialized.
    – Ivan Ivan
    Commented Feb 2, 2023 at 10:25
0

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');
2
  • 2
    don't you think this goes against best practices to alter core files. Commented Jan 5, 2015 at 12:52
  • in my situation it breaks WP :)
    – Someone
    Commented Jan 6, 2015 at 8:37
0

NOT wp-includes but :

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

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

-1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.