Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Just a note: is_admin() checks if you are in back or front end, it has nothing to do with the user being "admin". Opposed to is_super_admin(), which indeed checks for the user role. Also, please don't Multi-post the same Question in different Stack sites. –  brasofilo Mar 27 '13 at 12:11
add comment

1 Answer

up vote 1 down vote accepted

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

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' );
}
share|improve this answer
    
Many thanks toscho. I gave up at this point but now it did my task. –  Husnain Mar 27 '13 at 10:05
    
@Husnain See my update; I have added a simpler version. –  toscho Mar 27 '13 at 11:33
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.