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.

Does someone know if it's possible to display a specific sidebar per user-role ? (only accessible if the user is logged as the specific role related to the specific sidebar )?

What I want is to have one "profile-like" page in the front end with a custom sidebar per role (with custom nav, custom content, custom links...)

i.e : role A access to the "profile" page and has sidebar A, role B access to same page but has sidebar B...

Thanks for any help!

share|improve this question

2 Answers 2

up vote 0 down vote accepted

You could either read the role of the current user, then show the desired sidebar elements, or (what I'd prefer) check for certain capabilities.

User Role

global $current_user;
$roles = $current_user->roles;
$role = array_shift( $roles );
switch ( $role ) {
    case ...
}

Capabilities

if ( current_user_can( SOME_CAP ) ) {
} elseif ( current_user_can( SOME_CAP ) ) {
} else { //maybe display nothing or an error or whatever}
share|improve this answer
    
I will try with capabilities check and define a sidebar for each, thank you ! –  p36p Mar 11 '13 at 12:27

I actually have a feature like this in my Framework which does exactly that , Sidebar per user role, here is a quick plugin ported version of it which will create a sidebar per user role and automatically displaying the sidebar based on the current user's role

<?php
/*
Plugin Name: Sidebar Per User Role
Plugin URI: http://en.bainternet.info
Description: This Plugin lets you display a sidebar per user role
Version: 0.1
Author: Bainternet
Author Email: admin@bainternet.info
License:

  Copyright 2013 Bainternet (admin@bainternet.info)

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License, version 2, as 
  published by the Free Software Foundation.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

*/

if (!class_exists('SidebarPerRole')){
    /**
    * SidebarPerRole
    * @author Ohad Raz <admin@bainternet.info>
    * a class to register and display a sidebar per user role
    */
    class SidebarPerRole
    {   
        /**
         * $before_widget
         * @var string
         * @access public
         */
        public $before_widget = '<li id="%1$s" class="widget %2$s">';
        /**
         * $after_widget 
         * @var string
         * @access public
         */
        public $after_widget  = '</li>';
        /**
         * $before_title
         * @var string
         * @access public
         */
        public $before_title  = '<h2 class="widgettitle">';
        /**
         * $after_title 
         * @var string
         * @access public
         */
        public $after_title   = '</h2>';
        /**
         * $class
         * @var string
         * @access public
         */
        public $class = 'user-sidebar';
        /**
         * $sidebar_to_replace
         * @var string
         * @access public
         */
        public $sidebar_to_replace = 'guest-sidebar';

        /**
         * class constructor
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @param array $args array of arguments ex:
         *  class - CSS class name to assign to the widget HTML (default: user-sidebar).
         *  before_widget - HTML to place before every widget(default: '<li id="%1$s" class="widget %2$s">')
         *  after_widget - HTML to place after every widget (default: "</li>\n").
         *  before_title - HTML to place before every title (default: <h2 class="widgettitle">).
         *  after_title - HTML to place after every title (default: "</h2>\n").
         */
        function __construct($args = array()){
            //set defaults
            $this->set_props($args);
            //set hooks
            $this->hooks();
        }

        /**
         * set_props sets default and user defined properties
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @param array   $args       user defined properties
         * @param boolean $properties optional array od specific properties to set
         */
        function set_props($args = array(), $properties = false){
            if (!is_array($properties))
                $properties = array_keys(get_object_vars($this));

            foreach ($properties as $key ) {
              $this->$key = (isset($args[$key]) ? $args[$key] : $this->$key);
            }
        }

        /**
         * hooks function to hook all needed actions and filters
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @return void
         */
        function hooks(){
            //register sidebars
            add_action( 'widgets_init',array($this, 'register_sidebars'),100);

            //replace sidebars
            add_action('wp_head',array($this,'replace_sidebars'));
        }

        /**
         * register_sidebars function that registers a sidebar per user role and a guest sidebar
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @return void
         */
        function register_sidebars(){
            $roles = $this->get_editable_roles();
            //add sidebar per role
            foreach ((array)$roles as $key => $r) {
                $args = array(
                    'name'          => $r['name'] .__( ' Sidebar' ),
                    'id'            => str_replace(' ', '_' ,$key) .'-sidebar',
                    'description'   => __('Sidebar For ').$r['name'] .__(' Role Users'),
                    'class'         => $this->class . ' '. str_replace(' ', '_' ,$key) .'-sidebar',
                    'before_widget' => $this->before_widget,
                    'after_widget'  => $this->after_widget,
                    'before_title'  => $this->before_title,
                    'after_title'   => $this->after_title,
                );
                register_sidebar( $args );
            }
            //add guest sidebar
            $args = array(
                'name'          => __( 'Guest Sidebar' ),
                'id'            => 'guest-sidebar',
                'description'   => __('Sidebar For Guests'),
                'class'         => $this->class . ' ' . 'guest-sidebar',
                'before_widget' => $this->before_widget,
                'after_widget'  => $this->after_widget,
                'before_title'  => $this->before_title,
                'after_title'   => $this->after_title,
            );
            register_sidebar( $args );
        }

        /**
         * get_editable_roles gets an array of defined iser role
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @return array an array of user roles
         */
        function get_editable_roles() {
            global $wp_roles;

            $all_roles = $wp_roles->roles;
            $editable_roles = apply_filters('editable_roles', $all_roles);

            return $editable_roles;
        }

        /**
         * replace_sidebars the magic function which replaces the sidebar based on the current user role
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @return void
         */
        function replace_sidebars(){
            global $_wp_sidebars_widgets, $post, $wp_registered_sidebars, $wp_registered_widgets;
            //exit early if user is a guest
            if (!is_user_logged_in())
                return;

            $role = $this->get_user_role();
            $sidebar_id = str_replace(' ', '_' ,strtolower($role)) .'-sidebar';
            /*var_dump($sidebar_id);
            var_dump($_wp_sidebars_widgets);
            die();*/
            if ($role && isset($_wp_sidebars_widgets[$sidebar_id]) && count($_wp_sidebars_widgets[$sidebar_id]) >0 ){
                $_wp_sidebars_widgets[$this->sidebar_to_replace] = $_wp_sidebars_widgets[$sidebar_id];
            }
        }

        /**
         * get_user_role function to get a user role
         * @author Ohad Raz <admin@bainternet.info>
         * @access public
         * @return string role name
         */
        function get_user_role(){
            global $wp_roles;
            $current_user = wp_get_current_user();
            $roles = $current_user->roles;
            $role = array_shift($roles);
            return isset($wp_roles->role_names[$role]) ? $wp_roles->role_names[$role] : false;
        }
    }//end class
}//end if
global $sidebars_per_role;
$sidebars_per_role = new SidebarPerRole();

Usage:

Either call the guest sidebar in your theme using :

<?php dynamic_sidebar( 'guest-sidebar' ); ?>

which will be replaced based on the user role.

Or use an existing sidebar you want replaced by adding this in your themes functions.php

add_action('after_theme_setup','replace_sidebar_wpa_90315');
function replace_sidebar_wpa_90315(){
    global $sidebars_per_role;
    $sidebars_per_role->sidebar_to_replace = 'ID-OF-YOUR-Sidebar';
}

Update plugin created

share|improve this answer

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.