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条回答
ら.Afraid
2楼-- · 2019-01-18 02:38

try adding also

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

along with

require_once(ABSPATH.'wp-includes/pluggable.php');
查看更多
够拽才男人
3楼-- · 2019-01-18 02:38

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>';
        }        
    }
}
查看更多
Ridiculous、
4楼-- · 2019-01-18 02:39

my issue solved with this code please

include_once(ABSPATH . 'wp-includes/pluggable.php');
查看更多
Bombasti
5楼-- · 2019-01-18 02:41

You can use this,

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

this should fix your problem :)

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-18 02:41

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.

查看更多
再贱就再见
7楼-- · 2019-01-18 02:48

NOT wp-includes but :

include_once(ABSPATH . "wp-admin/includes/plugin.php");
查看更多
登录 后发表回答