wordpress plugin -> Call to undefined function wp_get_current_user()

后端 未结 9 1821
忘掉有多难
忘掉有多难 2020-12-01 07:58

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

相关标签:
9条回答
  • 2020-12-01 08:08

    try adding also

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

    along with

    require_once(ABSPATH.'wp-includes/pluggable.php');
    
    0 讨论(0)
  • 2020-12-01 08:12

    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>';
            }        
        }
    }
    
    0 讨论(0)
  • 2020-12-01 08:15

    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();
      // ...
    }
    
    0 讨论(0)
  • 2020-12-01 08:17

    my issue solved with this code please

    include_once(ABSPATH . 'wp-includes/pluggable.php');
    
    0 讨论(0)
  • 2020-12-01 08:17

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

    0 讨论(0)
  • 2020-12-01 08:18

    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.

    0 讨论(0)
提交回复
热议问题