categories widget based on user role (7 posts)

  1. deyelicious
    Posted 3 years ago #

    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?

  2. bcworkz
    Posted 3 years ago #

    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.

  3. deyelicious
    Posted 3 years ago #

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

  4. bcworkz
    Posted 3 years ago #

    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.

  5. keesiemeijer
    Posted 3 years ago #

    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() ) { 
    			// do stuff for admins
    			// 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';

  6. deyelicious
    Posted 3 years ago #

    @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?

  7. deyelicious
    Posted 3 years ago #

    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..

Topic Closed

This topic has been closed to new replies.

About this Topic