Fatal error: Call to undefined function wp_get_current_user()

I have some strange error or may be I do not have the skills to tackle this issue.
I am building a plugin for Multisite. When is use is_admin(), my plugin works fine but when I use is_super_admin it shows me this error Fatal error: Call to undefined function wp_get_current_user(). I did my search but could not be able to find any solution.

My Code is this

if(!is_super_admin()){
    add_action('widgets_init','my_unregister_widdget');
    function my_unregister_widgets() {
        unregister_widget( 'WP_Widget_Pages' );
        unregister_widget( 'WP_Widget_Calendar' );
    }
}

I saw this question but it’s not helping me.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

wp_get_current_user() is a pluggable function and not yet available when your plugin is included. You have to wait for the action plugins_loaded:

Example:

add_action( 'plugins_loaded', 'wpse_92517_init' );

function wpse_92517_init()
{
    if(!is_super_admin())
        add_action('widgets_init','my_unregister_widget');
}

function my_unregister_widgets() {
    unregister_widget( 'WP_Widget_Pages' );
    unregister_widget( 'WP_Widget_Calendar' );
}

Or move the check into the widget function:

add_action( 'widgets_init', 'my_unregister_widget' );

function my_unregister_widgets() 
{
    if ( is_super_admin() )
        return;

    // not super admin
    unregister_widget( 'WP_Widget_Pages' );
    unregister_widget( 'WP_Widget_Calendar' );
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x