wordpress plugin -> Call to undefined function wp_

2019-01-18 02:03发布

问题:

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?

回答1:

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


回答2:

You can use this,

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

this should fix your problem :)



回答3:

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.



回答4:

try adding also

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

along with

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


回答5:

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


回答6:

my issue solved with this code please

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


回答7:

NOT wp-includes but :

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


回答8:

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


回答9:

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