0

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.

2
  • Are you trying to do this with custom widgets, or existing widgets? Commented Mar 3, 2013 at 4:32
  • This is with custom widgets only. I have removed all of the default widgets.
    – AltDan
    Commented Mar 3, 2013 at 9:24

2 Answers 2

4

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:

4
  • 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?
    – AltDan
    Commented Mar 3, 2013 at 11:38
  • @dclawson, is_admin() is meant for that, Answer updated.
    – brasofilo
    Commented Mar 3, 2013 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.
    – AltDan
    Commented Mar 22, 2013 at 11:42
  • What if you try to hide a whole widget area?
    – brasofilo
    Commented Mar 22, 2013 at 12:18
3

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

/**
* Hide_widgets_role_based
* @author Ohad Raz <[email protected]>
*/
class Hide_widgets_role_based{
    $has_selector = false;
    $js_selector = '';
    $roles_hide =array();
    /**
     * class constructor
     * @author Ohad Raz <[email protected]>
     * @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');
2
  • 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? Commented Aug 1, 2014 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? Commented Aug 1, 2014 at 6:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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