显示每个角色的特定侧边栏

时间:2013-03-11 作者:p36p

是否有人知道是否可以为每个用户角色显示特定的侧栏?(仅当用户作为与特定侧栏相关的特定角色登录时才可访问)?

我想要的是在前端有一个“类似个人资料”的页面,每个角色有一个自定义侧栏(带有自定义导航、自定义内容、自定义链接…)

i、 e:角色A访问“配置文件”页面并具有侧栏A,角色B访问同一页面但具有侧栏B。。。

谢谢你的帮助!

2 个回复
最合适的回答,由SO网友:tfrommen 整理而成

您可以阅读当前用户的角色,然后显示所需的侧栏元素,或者(我更喜欢)检查某些功能。

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}

SO网友:Bainternet

实际上,我的框架中有一个这样的功能,每个用户角色的侧边栏,这是一个快速的插件移植版本,它将创建每个用户角色的侧边栏,并根据当前用户的角色自动显示侧边栏

<?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();
用法:

使用以下方法调用主题中的来宾侧栏:

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

将根据用户角色替换。

或者使用现有的边栏,通过在主题函数中添加此边栏来替换边栏。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\';
}
Updateplugin created

结束

相关推荐

Single Widget Multi Sidebar

我编写了一个小部件,它基于类型下拉列表具有多种输出格式。我打算在左侧和右侧边栏上使用这个小部件。然而,问题是它只显示在左侧或右侧的一个侧栏上。我希望小部件显示在两侧栏上请在下面找到小部件代码: class Posts_Widgets extends WP_Widget{ function __construct(){ $id = \"posts_widgets\"; $name = \"Anzima Posts Widgets\"; $