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: [email protected]
License:
Copyright 2013 Bainternet ([email protected])
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 <[email protected]>
* 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 <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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 <[email protected]>
* @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