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 would like to render some custom dashboard widget conditionally. If logined user have more than or equal to editor role I would like to render some more widgets than a user with subscriber role. How to do this? Any help please...

share|improve this question

1 Answer 1

up vote 1 down vote accepted
// PHP 5.3 syntax - anonymous functions (Closures)
add_action('wp_dashboard_setup', function(){
    if(current_user_can('activate_plugins')){ // Administrator
        wp_add_dashboard_widget('dbwidget-Administrator', '#Administrator', function(){
            // Print widget here
        });
    }elseif(current_user_can('delete_others_posts')){ // Editor
        wp_add_dashboard_widget('dbwidget-Editor', '#Editor', function(){
            // Print widget here
        });
    }elseif(current_user_can('delete_published_posts')){ // Author
        wp_add_dashboard_widget('dbwidget-Author', '#Author', function(){
            // Print widget here
        });
    }elseif(current_user_can('edit_posts')){ // Contributor
        wp_add_dashboard_widget('dbwidget-Contributor', '#Contributor', function(){
            // Print widget here
        });
    }elseif(current_user_can('read')){ // Subscriber
        wp_add_dashboard_widget('dbwidget-Subscriber', '#Subscriber', function(){
            // Print widget here
        });
    }
});

As you can see I chained them with elseif to ensure smooth Role degradation as Admin can do all the below too. Play with it as you wish. Learn more about Roles and Capabilities here.

share|improve this answer
    
thank you for your response. let me try –  notme Oct 11 '11 at 5:01

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.