WordPress.org

Support

Support » Plugins and Hacks » Hacks » categories widget based on user role

categories widget based on user role

  • deyelicious
    Member

    @deyelicious

    i have an issue..
    can i have different categories widget based on role user?
    for example :
    if user is guest, widget categories show only news and attention category
    if user is admin, show news, attention, update category..
    can anyone help me?

Viewing 6 replies - 1 through 6 (of 6 total)
  • bcworkz
    Participant

    @bcworkz

    Sure you can, but you’ll need a custom widget. You can either copy the original definition and rename it; or extend it. Once everything is set up, in the admin panel, remove the original widget and replace it with your custom version.

    In the widget definition, find the portion that outputs categories content to the browser. Wrap it with a conditional construct such as if (!$category == 'update' || current_user_can('manage_options'))
    Note it is easier to control output by user capability rather than by role.

    deyelicious
    Member

    @deyelicious

    tq for your quick response but i made some role which have same capability,,so i must control output by role,,
    but tq for your response 🙂

    bcworkz
    Participant

    @bcworkz

    Roles will work too. Same concept, just a bit more code to get roles, not a big deal. Alternately, add custom capabilities specifically to manage widget output, which is the preferred WordPress way of dealing with such things.

    But it’s your site, do what works for you.

    keesiemeijer
    Moderator

    @keesiemeijer

    The categories widged uses wp_list_categories() to display the categories. You can include or exclude categories with a filter. Example [untested]:

    add_filter('widget_categories_args', 'widget_category_role');
    
    function widget_category_role($args) {
    
      // check if user is logged in
    	if ( is_user_logged_in() ) { 
    
    		if(current_user_can('administrator')){
    			// do stuff for admins
    		}
    
    		if(current_user_can('guest')){
    			// exclude category IDs for the Role "guest".  (comma seperated list)
    			$args['exclude'] = '21,12,14';
    		}
    
    	} else {
    		// do stuff for non logged in users
    	}
    
      return $args;
    }

    Change the category IDs you want to exclude for the “guest” Role in $args['exclude'] = '21,12,14';

    deyelicious
    Member

    @deyelicious

    @bcworkz : hmm okay,,i got it,,i’ll little bit explore again
    @keesiemeijer : i’ll try your code..i can use it in wp-include/widgets.php? or in themes/functions.php?

    deyelicious
    Member

    @deyelicious

    hmm, i guess i already have a solution,,
    i got this plugin and it’s very useful http://wordpress.org/extend/plugins/nav-menu-roles/
    so, i made one menu from categories and arranged them based on role
    i show that menu with custom menu widget
    and puff, it display based on role..

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘categories widget based on user role’ is closed to new replies.