Skip Navigation

[Resolved] How to disable toolset buttons on post content fields for non-admins?

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to disable the toolset buttons on the frontend for specific user roles.

Solution:

Add the following to your functions.php file.

function remove_toolset_buttons(){
  
    $roles = array( 'administrator' ); // roles that can see the button

    $user = wp_get_current_user();
  
    if ( !in_array( $user->roles[0], $roles ) ) {
        // 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(){
            $roles2 = array( 'author', 'subscriber' );
            return $roles2;
        } );
}
add_action( 'init', 'remove_toolset_buttons' );

Now for the $roles variable this is an array of the users that should see the button.

For $roles2 this is an array of users to disable the button for.

This support ticket is created 6 years, 7 months ago. 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.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 9:00 – 12:00 -
- 13:00 – 18:00 13:00 – 18:00 13:00 – 18:00 14:00 – 18:00 13:00 – 18:00 -

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 15 replies, has 3 voices.

Last updated by Oliver 6 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#703395
toolset ticket 2.png
toolset ticket 1.png

This issue was addressed previously here: https://toolset.com/forums/topic/how-can-remove-toolset-buttons-from-cred-forms/#post-696227.

The tooset fields and access buttons should really be hidden from non-admins when using a post content field. The buttons only show up when the option to allow media uploads is checked for the cred submission field. It would be nice if there was a checkbox to disable toolset buttons.

I added the code to my functions.php file and some of the buttons disappeared, but the Access button is still there. Perhaps the plugin code has changed since that issue was resolved? Also, it seems like this code should still allow admins to see all the buttons, but for me it's the same for admins and subscribers - only the Access button is showing. The attached pics show before and after.

Another user submitted this ticket https://toolset.com/de/forums/topic/i-am-trying-to-remove-the-access-button-from-the-text-editor-on-the-frontend/ but I'm not making the same mistake - I've tested the site with both a subscriber and an admin user login.

I'm curious to know why toolset field and access buttons would be shown to regular users by default. Surely that's stuff that only an admin should see?

#705098

The toolset fields and access buttons should really be hidden from non-admins when using a post content field. The buttons only show up when the option to allow media uploads is checked for the cred submission field. It would be nice if there was a checkbox to disable toolset buttons.

This is partially true and I already filed this exact request a while ago.
Our Buttons should not rely on the insert media capability.
However, it's wrong to assume that non-administrators should not see those.
Everyone who can create and edit posts should see them as they are a substantial part of the building part.

However I as well filed requests to fine tune this control (simply add a capability around those buttons, that you can manage in Access later, it's not a big magic actually, but a question of decisions that I cannot make myself).

Now, for now, you need either to use Custom Roles or Custom Code to fine tune the control

There should be several possible solutions, one for backend the other for front end.

1. Toolset buttons (Fields and Views, CRED, Access) are visible on front-end editors for users with role author+ and when the insert media option is enabled.

You can disable it with the following code:

/**
* Remove Toolset buttons on front-end editors
* which appear for role author+ when the 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 );
	$target_pages = array( 10, 25, 66 ); // Edit for pages with CRED forms


	if ( in_array( $postID, $target_pages ) ) {
		// 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', 'subscriber' );
			return $roles;
		} );
	}
}
add_action( 'init', 'remove_toolset_buttons' );

2. This code allows you to Remove buttons (fields and views, Content Layout Editor, CRED form, Access, Bootstrap buttons) from backend WP editor for certain user roles.

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 an example, you can replace those with your roles as needed. These are in the if conditional statement.

Then, download & extract this file, and place this CSS file in the wp-content/themes/your_theme/ (inside your theme) folder in your site:
hidden link

#705457

Thanks! I used the first method as I don't allow users the ability to create posts from the back end. Works great now.

Yes, of course you're right about allowing more than administrators. I misused the term "administrator" to mean higher-level managers like editors.

#705570

Actually, could you also post the code that would allow selective enabling of the cred and fields and views buttons, as you have done with the access button? Thanks!

#705772

I don't follow your last request.

If you mean the check of the role, within the filter, that's pure PHP and WordPress API.
You can either return nothing or some user roles in the array as seen.

#705885

Changing status

#708029

What I meant was that this code allows you to display the access button for certain roles but makes the other buttons disappear for everyone. Could the same option to show by user role be applied for the cred/fields and views buttons? I'm curious why you only applied the optional view for the access button.

#729653

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Oliver,

I will be handling this ticket now but i'm not sure I follow.

The code beda sent has disabled the buttons for you correct? But now you want the buttons to show for other roles ?

Please let me know.
Thanks,
Shane

#741194

Hi Shane, in this section of code the third part allows the ability to remove the access button for certain roles. But the first two parts don't have that functionality. I just wanted to make sure that I could do the same thing to the first two. And I was wondering why Beda coded it this way, rather than making it the same for all three.

// 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', 'subscriber' );
            return $roles;

Am I right in assuming I can change the first two like this?

// remove the Fields and Views button for certain roles
        add_filter( 'toolset_editor_add_form_buttons', function(){
            $roles = array( 'author', 'subscriber' );
            return $roles;

        // remove the CRED button for certain roles
        add_filter( 'toolset_cred_button_before_print', function(){
            $roles = array( 'author', 'subscriber' );
            return $roles;
#745250

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Oliver,

So the only button that needs to be removed now is the Views button on the frontend correct?

Please let me know.
Thanks,
Shane

#746107

Hi Shane,

Not really. I'm trying to selectively (by user role) disable the fields/views button and the cred button as well as the access button. Currently only the access button can be selectively disabled by user role.

Is the code that I wrote correct?

#753316

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Oliver,

From what I can see you code is correct.

Does it not work ?

Could you try the following ?

function remove_toolset_buttons(){
 
    $roles = array( 'administrator' ); // roles to remove the buttons for
	$user = wp_get_current_user();
 
    if ( !in_array( $user->roles[0], $roles ) ) {
        // 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', 'subscriber' );
            return $roles;
        } );
}
add_action( 'init', 'remove_toolset_buttons' );

What you can do is to add the other roles that you want the items to show for and it will not show for the roles that are not in the list.

Thanks,
Shane

#760160

Yes thank you, your code worked!

Just a minor note, but I think the comment indicating the function of the array is incorrect. I think it should be something like this:

    $roles = array( 'administrator'); // roles that can see the buttons
#828695

Hello, I'm sorry to reopen this but I just noticed an error is being thrown on the page for non-logged in users:

"Notice: Undefined offset: 0" in line 6 in the last bit of code:

if ( !in_array( $user->roles[0], $roles ) ) {

Could you please fix? Thanks.

#843262

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Oliver,

Try adding this as well.

if ( !in_array( $user->roles[0], $roles ) || !(is_user_logged_in())) {

Thanks,
Shane