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'm looking for a way to hide certain widgets from specific user types in the admin, Appearance > Widgets page?

For instance, admins can see all widgets, but authors can only see a select few?

I noticed there was a Code Canyon plugin, but it requires a framework with it also which I am not using.

share|improve this question
    
Are you trying to do this with custom widgets, or existing widgets? –  Matt Van Andel Mar 3 '13 at 4:32
    
This is with custom widgets only. I have removed all of the default widgets. –  dclawson Mar 3 '13 at 9:24

2 Answers 2

up vote 2 down vote accepted

You have to check for Roles and Capabilities in the action hook widgets_init and proceed accordingly.

add_action( 'widgets_init',  'remove_widgets_wpse_89138' , 15 );

function remove_widgets_wpse_89138()
{
    // http://codex.wordpress.org/Function_Reference/is_admin
    if( !is_admin() )
        return;

    // Grab current user info
    global $current_user;

    // Check for specific user
    /*
    $username = $current_user->user_login;
    if( 'the_user_login' != $username)
        return;
    */

    // Check for capability
    if( current_user_can( 'add_users' ) )
        return;

    unregister_widget( 'WP_Widget_Pages' );
}

Q&A's of interest:

share|improve this answer
    
this works great thanks alot! The only problem I noticed is this also hides the widget from the front-end for that logged in user. Is there a way to hide it in the CMS only? –  dclawson Mar 3 '13 at 11:38
    
@dclawson, is_admin() is meant for that, Answer updated. –  brasofilo Mar 3 '13 at 11:41
    
Ahhh yes, that works brilliantly. Thanks. –  dclawson Mar 3 '13 at 11:48
1  
Another question/ issue i've found with this. If I have hidden a number of widgets for certain user roles. When they save a widget on the widgets page they hidden ones seem to be removed from the website. –  dclawson Mar 22 '13 at 11:42
    
What if you try to hide a whole widget area? –  brasofilo Mar 22 '13 at 12:18

I actually have a class which i use just for that:

/**
* Hide_widgets_role_based
* @author Ohad Raz <admin@bainternet.info>
*/
class Hide_widgets_role_based{
    $has_selector = false;
    $js_selector = '';
    $roles_hide =array();
    /**
     * class constructor
     * @author Ohad Raz <admin@bainternet.info>
     * @param array $args [description]
     */
    function __construct($args = array()){
        if (is_admin())
            add_action('widgets_init', array($this,'hide', 999));
    }

    /**
     * the money function that hides the widgets on the admin side when the user has a specific role
     * @return void
     */
    public function hide(){
        global $pagenow;
        if ($pagenow == 'widgets.php'){
            global $current_user;
            get_currentuserinfo();
            $sperator = "";
            foreach ($this->roles_hide as $role => $widgets) {
                if ($this->has_role($role)){
                    foreach ((array)$widgets as $w_id) {
                        unregister_widget( $w_id );
                    }
                }
            }
        }
    }


    /**
     * add a widget to hide per role
     * @param string $role   role name
     * @param string $widget widget id
     */
    public function addHide($role,$widget){
        if (is_array($widget)){
            $tmp = isset($roles_hide[$role])? $roles_hide[$role]: array();
            $roles_hide[$role] = array_merge($tmp, (array)$widget);
        }else{
            $roles_hide[$role][] = $widget;
        }
    }

    /**
     * has_role check if a user has a role
     * @param  int  $user_id user id
     * @return boolean          
     */
    public function has_role($user_id = null){
        if ( is_numeric( $user_id ) )
            $user = get_userdata( $user_id );
        else
            $user = wp_get_current_user();

        if ( empty( $user ) )
        return false;

        return in_array( $role, (array) $user->roles );
    }
}

Usage:

$widgets_hide = new Hide_widgets_role_based();
$widgets_hide->addHide('contributor',array('WP_Widget_Pages','WP_Widget_Calendar','WP_Widget_Links'));
$widgets_hide->addHide('editor','WP_Widget_Links');
share|improve this answer
    
Interesting. In the usage where does $widgets_hide = new Hide_widgets_role_based(); hook into? Are you suppose to put it all in it's own function and call it a seperate action? –  willow Aug 1 at 6:12
    
Also the accepted answer doesn't work because if another user adds a widget to a sidebar that has an unregistered widget in it, it will get removed after the sidebar gets updated. Does your solution fix that? –  willow Aug 1 at 6:16

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.