Hide for user roles, Widget name
-
Is it possible to disable FE for user roles like “Guest” ?
If have seen snippets which can do that but I have no clue what official Widget name FE has. Something like “WP_Widget_Product_Tag_Filter” maybe?
Maybe it is also a suggestion for a new option to improve FE.
-
Hi
You can use this small code snippen to get all of the widgets IDs:
function display_widget_ids() { global $wp_registered_widgets; // Get the names of all active widgets $active_widgets = get_option('sidebars_widgets'); echo '<pre>'; foreach ($active_widgets as $widget_area => $widgets) { if (!empty($widgets)) { echo "Widgets in '$widget_area':\n"; foreach ($widgets as $widget_id) { echo " - $widget_id\n"; } echo "\n"; } } echo '</pre>'; } add_action('wp_footer', 'display_widget_ids');Our filter widget should be wpc_filters_widget
Our plugin does not have any restrictions for user roles, so it can be only done by other plugins or custom functionality.
Best Regrds – Victor
Wow. Awesome. If it worked for me. 🙁
Neither it displays widget names nor hides the widget.
Found this snipped to hide widgets for user roles:
/** * Hide_widgets_role_based * @author Ohad Raz <[email protected]> */ class Hide_widgets_role_based{ $has_selector = false; $js_selector = ''; $roles_hide =array(); /** * class constructor * @author Ohad Raz <[email protected]> * @param array $args [description] */ function __construct($args = array()){ if (is_admin()) add_action('widgets_init', array($this,'hide', 999)); } /** * the money function that hides the widgets on the admin side when the user has a specific role * @return void */ public function hide(){ global $pagenow; if ($pagenow == 'widgets.php'){ global $current_user; get_currentuserinfo(); $sperator = ""; foreach ($this->roles_hide as $role => $widgets) { if ($this->has_role($role)){ foreach ((array)$widgets as $w_id) { unregister_widget( $w_id ); } } } } } /** * add a widget to hide per role * @param string $role role name * @param string $widget widget id */ public function addHide($role,$widget){ if (is_array($widget)){ $tmp = isset($roles_hide[$role])? $roles_hide[$role]: array(); $roles_hide[$role] = array_merge($tmp, (array)$widget); }else{ $roles_hide[$role][] = $widget; } } /** * has_role check if a user has a role * @param int $user_id user id * @return boolean */ public function has_role($user_id = null){ if ( is_numeric( $user_id ) ) $user = get_userdata( $user_id ); else $user = wp_get_current_user(); if ( empty( $user ) ) return false; return in_array( $role, (array) $user->roles ); } } $widgets_hide = new Hide_widgets_role_based(); $widgets_hide->addHide('guest','wpc_filters_widget');Not resolved. that’s why I said “If it worked for me :-(“
Hi @mailchef
You can use something very simple like this:
function show_widget_for_admin() { // Check if a user is logged in if (is_user_logged_in()) { // Get the current user object $current_user = wp_get_current_user(); // Check if the current user has the 'administrator' role or any other needed one if (in_array('administrator', $current_user->roles)) { // Display the shortcode of FE widget echo do_shortcode( '[fe_widget]' ); } } } // Hook the function to a needed place for example it was hooked to the footer add_action('wp_footer', 'show_widget_for_admin');It will show the shortcode of the FE widget or not, depending on the user role, very simple yet effective.
Best Regards – Victor
The topic ‘Hide for user roles, Widget name’ is closed to new replies.