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.

  • 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

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() )

    // Grab current user info
    global $current_user;

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

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

    unregister_widget( 'WP_Widget_Pages' );

Q&A's of interest:

| 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
  • 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;
            $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);
            $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 );
            $user = wp_get_current_user();

        if ( empty( $user ) )
        return false;

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


$widgets_hide = new Hide_widgets_role_based();
| 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? – Bryan Willis Aug 1 '14 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? – Bryan Willis Aug 1 '14 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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