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 think I'm pretty close to what I need, but just need a little direction. I'm trying to show a content widget in the backend to users of a certain role. Right now, I'm just testing it out with a user that has a role of subscriber.

This works perfect :

<?php 
    // Add a widget to the WordPress dashboard
    function wpc_dashboard_widget_function() 
    {
        // Do whatever you want to render in here
        echo '<div>
        <h3>Special Offer One</h3>
        <div>Special Offer Text will go here!</div>
        </div>';
    }
    function wpc_add_dashboard_widgets() 
    {
        wp_add_dashboard_widget('wp_dashboard_widget', 'Special Offers Just For   
        Vendors', 'wpc_dashboard_widget_function');
    }
    add_action('wp_dashboard_setup', 'wpc_add_dashboard_widgets' );
?>

But that shows it to all users. I would like to only show to a certain role and was trying to use:

if (!current_user_can('subscriber')):
endif;

along with it, but it just screws things up and the special widget doesn't show at all for anyone. Any ideas?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

Possible Reasons -

  1. current_user_can() function needs an input of a capability not user role, though it works sometime but we should not use user role as a input to this function.
  2. you're passing the subscriber object to function which is a lowest possible role we can have on Wordpress. That's why !current_user_can('subscriber') makes it unavailable for all.

Have a look at Wordpress Documentation of Capabilities Vs Roles table, Use that table to determine which capability you can use to hide it or show to particular role. To show the box to Editor you can use capability moderate_comments .

E.g -   if ( current_user_can ('moderate_comments') ) 
        {
            //To see this visible you should have at-least Editor privileges
        } 

Note -

I recommend using wp_get_current_user() function. This can be used to grab user role and show specific content. The content will be available to that user only, Not even users with higher privileges can see it.

   // Add a widget to the WordPress dashboard
    function wpc_dashboard_widget_function() 
    {
        global $wp_roles;
        $current_user = wp_get_current_user();
        $roles = $current_user->roles;
        $role = array_shift($roles);

        if($role == 'administrator')
        {
            // This is only for Admins
            echo '<div><h4>Special Offer One</h4><div>Special Offer Text will go here!</div></div>';
        }
    }
    function wpc_add_dashboard_widgets() 
    {
        wp_add_dashboard_widget('wp_dashboard_widget', 'Special Offers Just For   
        Vendors', 'wpc_dashboard_widget_function');
    }
    add_action('wp_dashboard_setup', 'wpc_add_dashboard_widgets' );
share|improve this answer
    
Ok, great! Thank you! The first example with using if current_user_can works great, and thank you for explaining it to me. I did try to use your noted, bottom recommendation, but it didn't work, really. It shows to all users, plus didn't echo out the full html. –  Anthony Myers Aug 6 '12 at 2:50
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.