Daithí
Daithí

Reputation: 4079

wordpress plugin -> Call to undefined function 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()

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?

Upvotes: 46

Views: 64162

Answers (12)

Highly Irregular
Highly Irregular

Reputation: 40499

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

Upvotes: 0

Vic Seedoubleyew
Vic Seedoubleyew

Reputation: 10526

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

Upvotes: 1

Farasat
Farasat

Reputation: 11

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

Upvotes: 1

epicrato
epicrato

Reputation: 8408

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

Upvotes: 1

T.Todua
T.Todua

Reputation: 56351

NOT wp-includes but :

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

Upvotes: 0

Mikhail Villamor
Mikhail Villamor

Reputation: 26

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

Upvotes: -1

Kapil Goyal
Kapil Goyal

Reputation: 117

my issue solved with this code please

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

Upvotes: 6

Denis de Bernardy
Denis de Bernardy

Reputation: 78413

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();
  // ...
}

Upvotes: 74

Mau
Mau

Reputation: 1313

try adding also

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

along with

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

Upvotes: 5

colind
colind

Reputation: 9

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

Upvotes: 0

Benjamin Ziepert
Benjamin Ziepert

Reputation: 1754

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.

Upvotes: 4

user677607
user677607

Reputation:

You can use this,

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

this should fix your problem :)

Upvotes: 26

Related Questions