Skip Navigation

[Resuelto] Removing buttons for certain user roles for the post editor

Este hilo está resuelto. Aquí tiene una descripción del problema y la solución.

Problem:
Remove buttons (fields and views, Content Layout Editor, CRED form, Access, Bootstrap buttons) from backend editor for certain user roles.

Solution:
1. Add this code in your theme’s or child theme’s functions.php file:

add_filter( 'mce_buttons_3', 'remove_bootstrap_buttons', 999 );
function remove_bootstrap_buttons($buttons) {
    if( current_user_can('editor') || current_user_can('contributor') ) { 
        return array();
    }
    else {
        return $buttons;
    }
}
   
add_filter( 'mce_buttons', 'remove_toggle_button', 999 );
function remove_toggle_button($buttons) {
    if( current_user_can('editor') || current_user_can('contributor') ) { 
        $remove = array( 'css_components_toolbar_toggle' );
        return array_diff( $buttons, $remove ); 
    }
    else {
        return $buttons;
    }
}
 
function vm_wptypes_remove() {
     
    if( current_user_can('editor') || current_user_can('contributor') ) { 
        wp_enqueue_style( 'wp-types-special', get_stylesheet_directory_uri() . '/wp-types-special.css' );
 
    } 
}
add_action( 'admin_init', 'vm_wptypes_remove' );

==> In above code, I have used 'editor' and 'contributor' level roles as example, you can replace those with your roles as needed. These are in the if conditional statement.

2. Download & extract this file >> and place this css file in the folder in your site:
wp-content/themes/et/ (inside your theme folder)
CSS file:
https://drive.google.com/file/d/0B5EmJQ1qcuyqTU4xZ3paM01ERmc/view?usp=sharing

Relevant Documentation:
Related ticket: https://toolset.com/forums/topic/how-to-remove-fields-and-views-button-above-post-edit-for-non-admin-users/

This support ticket is created hace 6 años, 7 meses. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 12:00 – 17:00 -
- 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 18:00 – 21:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

Autor
Mensajes
#568994
buttons.jpg

I would like to be able to remove these buttons for certain user roles.
There are a number of pointers to this but my wordpress/php abilities are not sufficient to be able to put them together to make all this happen!
These are the articles from the toolset support which explain how to do something like this:

https://toolset.com/forums/topic/additional-button-in-latest-version-of-cred/

/**
* Remove Toolset buttons on front-end editors
* which appear for role author+ when insert
* media option set on CRED forms
*
* The filters work globally, so you will need
* to add a test, e.g. for the page where the
* CRED form is added
*/
function remove_toolset_buttons(){
 
// $post not available with init hook
$postID = url_to_postid( $_SERVER['REQUEST_URI'] , '_wpg_def_keyword', true );
 
if ( 19 == $postID ) { // edit with your own condition
// remove the Fields and Views button
add_filter( 'toolset_editor_add_form_buttons', '__return_false' );
 
// remove the CRED button
add_filter( 'toolset_cred_button_before_print', '__return_false' );
 
// remove the Access button for certain roles
add_filter( 'toolset_editor_add_access_button', function(){
$roles = array( 'author' );
return $roles;
} );
}
}
add_action( 'init', 'remove_toolset_buttons' );

I want to remove all the buttons for a user role. I also want to remove the boostrap stuff and the conditional button. I also want to remove the content layout button for that user role.

#569069

Noman
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Andrew,

Thank you for contacting Toolset support. The example ticket & code you are refering to is about frontend Buttons in CRED form, whereas you need to hide these on backend and for specific role.

If you want to remove or hide Bootstrap buttons / Bootstrap-Components / Bootstrap toggle button (tinymce buttons) from WP Post Editors. There are two ways to do this:

1. If your site is not using Bootstrap, then you can disable it by going to WordPress dashboard >> Settings >> General >> Bootstrap loading >> This site is not using Bootstrap CSS — please see screenshot:
hidden link

2. If your site is using Bootstrap, then you can disable it by adding following code in the theme’s or child theme’s functions.php file to remove Bootstrap buttons from editor:

add_filter( 'mce_buttons_3', 'remove_bootstrap_buttons', 999 );
function remove_bootstrap_buttons($buttons) {
    return array();
}
  
add_filter( 'mce_buttons', 'remove_toggle_button', 999 );
function remove_toggle_button($buttons) {
    $remove = array( 'css_components_toolbar_toggle' );
    return array_diff( $buttons, $remove ); 
} 

I will check further and try to remove other buttons and for specific roles, then get back to you.
Thank you

#569132

Noman
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

I got the solution for it. Leave the above code and follow below steps to remove those buttons:

1. Please add this code in your theme’s or child theme’s functions.php file:

add_filter( 'mce_buttons_3', 'remove_bootstrap_buttons', 999 );
function remove_bootstrap_buttons($buttons) {
	if( current_user_can('editor') || current_user_can('contributor') ) { 
    	return array();
	}
	else {
		return $buttons;
	}
}
  
add_filter( 'mce_buttons', 'remove_toggle_button', 999 );
function remove_toggle_button($buttons) {
	if( current_user_can('editor') || current_user_can('contributor') ) { 
    	$remove = array( 'css_components_toolbar_toggle' );
    	return array_diff( $buttons, $remove ); 
	}
	else {
		return $buttons;
	}
}

function vm_wptypes_remove() {
	
	if( current_user_can('editor') || current_user_can('contributor') ) { 
        wp_enqueue_style( 'wp-types-special', get_stylesheet_directory_uri() . '/wp-types-special.css' );

	} 
}
add_action( 'admin_init', 'vm_wptypes_remove' );

==> In above code, I have used 'editor' and 'contributor' level roles as example, you can replace those with your roles as needed. These are in the if conditional statement.

2. Download & extract this file >> and place this css file in the folder in your site:
wp-content/themes/et/ (inside your theme folder)
CSS file: hidden link

I hope it helps, Thank you

#570594
layout.jpg

Hi Noman,

Apologies for delay....I did not tick the email box!
Brilliant! I am nearly there....
Is there a way of removing the Content Layout Editor button?

#570900

Noman
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

Hello Andrew,

I have updated the CSS file, please delete the last one and use this updated file:
hidden link

Thank you

#570907

Hi Noman,

That does not seem to have made any difference...the button is still there...

ANdrew

#570925

Noman
Supporter

Languages: Inglés (English )

Timezone: Asia/Karachi (GMT+05:00)

The CSS file works for me and hides the button. Please delete all sort of caches in your site plugin cache (is any), CDN / server cache (if any), browser cache too.

If above does not solve the issue, please provide temporary access (WP-Admin and FTP Login info) to your site (preferably staging site), so that I can look into your setup and check the issue.

Your next answer will be private which means only you and I have access to it.

=== Please backup your database and website ===

✙ I would additionally need your permission to de-activate and re-activate Plugins and the Theme, and to change configurations on the site. This is also a reason the backup is really important.

Thank you

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.