Hiding widgets for specific user roles - php

So its about hiding widgets for specific user roles excluding Admin.Using custom sidebar plugin i don't want to be display on users end.Listed all Dashboard widgets through
function list_active_dashboard_widgets() {
global $wp_meta_boxes;
foreach (array_keys($wp_meta_boxes['dashboard']['normal']['core']) as $name) {
echo '<div>' . $name . '</div>';
}
}
add_action('wp_dashboard_setup', 'list_active_dashboard_widgets');
and found customsidebars-mb
What i am doing without succes is adding this code to hide sidebar options widget from users panel keeping it on admin panel.
function disable_default_dashboard_widgets() {remove_meta_box('customsidebars-mb', 'dashboard', 'normal');
}
add_action('admin_menu', 'disable_default_dashboard_widgets');
if (!current_user_can('manage_options')) {
add_action('wp_dashboard_setup', 'disable_default_dashboard_widgets');
}
**
Store front Theme Woocommerce Plugin
**Will be phasing out all plugins with codes

You can try this one in your functions.php,
add_action( 'widgets_init', 'remove_widgets_wpse_89138' , 15 );
function remove_widgets_wpse_89138()
{
// http://codex.wordpress.org/Function_Reference/is_admin
if( !is_admin() )
return;
// Grab current user info
global $current_user;
// Check for specific user
/*
$username = $current_user->user_login;
if( 'the_user_login' != $username)
return;
*/
// Check for capability
if( current_user_can( 'add_users' ) )
return;
unregister_widget( 'WP_Widget_Pages' );
}
Hope this will helps you. For more please visit, URL 1, URL 2

Related

remove_submenu_page('woocommerce', 'wc-admin'); function not removing dashboard

I have created a new role in WordPress for WooCommerce. The only thing I require this role to see is:
WooCommerce --> Orders Only
Users - All Submenus
WP All Import - All Submenus except for Settings
WP All Export - All Submenus except for Settings
I successfully dwindled down to the list but am having issues isolating the WooCommerce Dashboard sub-menu.
I used this code to get a full list:
if (!function_exists('debug_admin_menus')):
function debug_admin_menus() {
if ( !is_admin())
return;
global $submenu, $menu, $pagenow;
if ( current_user_can('manage_options') ) { // ONLY DO THIS FOR ADMIN
if( $pagenow == 'index.php' ) { // PRINTS ON DASHBOARD
echo '<pre>'; print_r( $menu ); echo '</pre>'; // TOP LEVEL MENUS
echo '<pre>'; print_r( $submenu ); echo '</pre>'; // SUBMENUS
}
}
}
add_action( 'admin_notices', 'debug_admin_menus' );
endif;
I found that component being: wc-admin where I added:
remove_submenu_page('woocommerce', 'wc-admin');
BUT it isn't removing it. Does anyone know why? I've also tried:
add_action( 'admin_menu', 'remove_menu_pages', 999);
function remove_menu_pages() {
global $current_user;
$user_roles = $current_user->roles;
$user_role = array_shift($user_roles);
if($user_role == "custom_shop_admin") {
$remove_submenu = remove_submenu_page('woocommerce', 'wc-admin');
}
}
Everywhere I've looked up has ('woocommerce', 'wc-admin') as the dashboard. Not sure if I can't remove it, to redirect it to the orders page perhaps for that role only. I know there has been a recent WooCommerce Update so not sure if that has anything to do with it.
Thank you!
I know its been a long time no talk here. I had the same problem. I have figured it out and it may help people out in the future to solve the problem really quick. Here is the solution.
function remove_menus_custom(){
global $current_user;
if(is_admin(){
remove_submenu_page('woocommerce','wc-admin');
}
}
add_action( 'admin_menu', 'remove_menus_custom', 999 );

Custom my account endpoint in Woocommerce just for a specific user role

Following the woocommerce documentation, I added an endpoit to my-account page in woocommerce.
I want to make this endpoint visible only to a specific user role, lets say shop_manager.
Is there a way to redirect to a 404 page users who try to access directly that endpoint?
Thanks in advance.
Assuming that you have already created a custom endpoint to my account section (see this related answer), you can redirect all non allowed user roles to a specific page using template_redirect hook in this simple way:
add_action( 'template_redirect', 'custom_endpoint_redirection' );
function custom_endpoint_redirection() {
$allowed_user_role = 'administrator';
$custom_endpoint = 'my-custom-endpoint';
if ( is_wc_endpoint_url($custom_endpoint) && ! current_user_can($allowed_user_role) ) {
$redirection_url = home_url( '/404/' );
wp_redirect( $redirection_url );
exit;
}
}
You need to specify your custom end point, your allowed user role and the url redirection.
Code goes in functions.php file of your active child theme (or active theme). It could works.
Related:
WooCommerce - Assign endpoints to multiple custom templates in my-account page
WooCommerce: Assigning an endpoint to a custom template in my account pages
WooCommerce: Adding custom template to customer account pages
Custom my account new menu item for a specific user role in Woocommerce
Just add the follows code snippet in your active theme's functions.php and this is only for administrator user role, you can change it as per you -
function add_custom_my_account_endpoint() {
add_rewrite_endpoint( 'shop_manager', EP_PAGES );
}
add_action( 'init', 'add_custom_my_account_endpoint' );
function add_custom_wc_menu_items( $items ) {
$user = wp_get_current_user();
if( $user && in_array( 'administrator', $user->roles ) ){
$items[ 'shop_manager' ] = __( 'Shop Manager', 'text-domain' );
}
return $items;
}
add_filter( 'woocommerce_account_menu_items', 'add_custom_wc_menu_items' );
function add_shop_manager_endpoint_content(){
$user = wp_get_current_user();
if( $user && !in_array( 'administrator', $user->roles ) ) return;
echo "Your content goes here";
}
add_action( 'woocommerce_account_shop_manager_endpoint', 'add_shop_manager_endpoint_content' );
After this just flush_rewrite_rules from Backend Settings > Permalinks. Thats it.

Hide in Woocommerce Dashboard, for specific User Role 'Order' sub-menu from Woocommerce

if (!current_user_can('administrator')) {
function remove_admin_menus () {
global $menu;
$removed = array(
__('WooCommerce'),
);
end ($menu);
while (prev($menu)){
$value = explode(
' ',
$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $removed)){
unset($menu[key($menu)]);
}
}
}
}
add_action('admin_menu', 'remove_admin_menus');
This code hide the whole Woocommerce item from Wordpress dashboard, if you are Administrator, but i didn't fiind a solution to hide only Orders sub-menu, not the whole item.
Who has an idea?
You were taking global $menu instead $submenu. Then you will get a list of all submenus registered. You can add the following code. Also it is better to check if user is admin inside the function call
function remove_admin_menus(){
global $submenu;
if(current_user_can('administrator')){
unset($submenu['woocommerce']['1']);
}
}
add_action('admin_menu', 'remove_admin_menus');
UPDATE
Even if the menu is hidden, one can access the page if he knows the url. So inorder to block the access to url, add the following
function restrict_woo_submenu_userrole(){
$current_screen = get_current_screen();
$p_id = $current_screen->id;
if($p_id == 'edit-shop_order' && current_user_can('administrator')){
wp_die('Restricted Access.');
}
}
add_filter( 'current_screen', 'restrict_woo_submenu_userrole' );

Wordpress Author box Control Function

Hello friends,
i need little help in Wordpress. I am trying to hide the authorbox that appears under the post for specific user only.
Example if post i am looking into is posted by admin, then i want to hide the authorbox under post content that is posted by admin, but should show for all other users ? i tried different functions but not able to success on this.
I want to completly hide the authorbox, i know it can be done with css code that is
.author-box { display:none;}
but this hides the overall authorbox of complete them, i just want to hide the authorbox on the posts that are made by admin ?
i am using genesis framework, so please suggest if any help you can make here.
Thanks
In your theme's functions.php file, add something like this:
function remove_my_post_metabox() {
global $post;
// If the post author ID is 1, then remove the meta box
// You would need to find the ID of the author, then put it in place of the 1
if ( $post->post_author == 1) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );
If you need to find out the role of the author, and truly do it based on the role (admin, for example), then it would be more like this:
function remove_my_post_metabox() {
global $post;
// If there's no author for the post, get out!
if ( ! $post->post_author) {
return;
}
// Load the user
$user = new WP_User( $post->post_author );
// Set "admin" flag
$is_admin = FALSE;
// Check the user roles
if ( !empty( $user->roles ) && is_array( $user->roles ) ) {
foreach ( $user->roles as $role ) {
if ( $role == 'administrator' ) {
$is_admin = TRUE;
}
}
}
// Lastly, if they ARE an admin, remove the metabox
if ( $is_admin ) {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
}
}
add_action( 'add_meta_boxes', 'remove_my_post_metabox' );

Allow custom role to see specific section in wordpress

I am trying to allow the custom role bbp_moderator to view the Users Section from wordpress backend, but for some reason is not working. i can see the roles via the $wp_roles global variable and the capability edit_users had been added the the bbp_moderator role.
function add_capablilities_to_mod() {
$role = get_role('bbp_moderator');
$role->add_cap('edit_users');
}
function add_user_table_to_moderator() {
global $wp_roles;
//
//var_dump(current_user_can( 'bbp_moderator' )); -> true
if ( current_user_can( 'bbp_moderator' ) ) {
add_action( 'plugins_loaded', 'add_capablilities_to_mod' );
}
}
add_action('init', 'add_user_table_to_moderator');

Categories