[Resolved] how to remove "fields and views" button above post edit for non admin users?

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.

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

Problem:
How to remove Toolset Buttons like "Fields and View" for certain User roles from the WP Post/page editor

Solution:
1. Create Custom Role, or use WP Default Roles
2. Create a

wptypes-special.css

CSS file, in your Themes Folder
3. Add this CSS to that File:
For Views only

span.button.wpv-shortcode-post-icon.js-wpv-shortcode-post-icon-wpv-views{ 
    display:none !important; 
}

For Views AND CRED:

span.button.wpv-shortcode-post-icon.js-wpv-shortcode-post-icon-wpv-views, span#cred-form-shortcode-button.cred-media-button{ 
    display:none !important; 
}

4. Put this conditional in your functions.php:

function vm_wptypes_remove() {
    if ( current_user_can( 'your-role-or-capability' ) ) {
        wp_enqueue_style( 'wptypes-special', get_stylesheet_directory_uri() . '/wptypes-special.css' );
    }
}
add_action( 'admin_init', 'vm_wptypes_remove' );

DOC:
https://codex.wordpress.org/Function_Reference/current_user_can

Tagged: 

This topic contains 5 replies, has 2 voices.

Last updated by Beda 4 years, 4 months ago.

Assigned support staff: Beda.

Author
Posts
#335860

I am trying to: remove fields and views button from post edit screen for non-admins

I visited this URL:
https://toolset.com/forums/topic/remove-toolset-buttons-from-above-the-tinymce-editor/

I expected to see:
After implementing solution recommended on that link, the "Fields and Views" button still displays for non-admins who have the capability to add and publish new content/products.

Instead, I got:
The button is still there.

I added this to the child theme's functions.php

/* Hide WP-Types button from post edit for non admin users
Ref link: https://toolset.com/forums/topic/remove-toolset-buttons-from-above-the-tinymce-editor/
*/
add_action( 'admin_init', 'vm_wptypes_remove' );
function vm_wptypes_remove() {
    if ( ! current_user_can( 'update_core' ) ) {
        wp_enqueue_style( 'wptypes-special', get_stylesheet_directory_uri() . '/wptypes-special.css' );
    }
}

And, I added this one-line stylesheet to the child theme's folder:

#wp-content-media-buttons ul.editor_addon_wrapper, #cred-form-shortcode-button.cred-media-button { display:none !important; }

This code removed the Soliloquy button and I'm hoping WP-Types has a similar hook.

/*
Limit Soliloquy to admin users only
Ref Link: <em><u>hidden link</u></em>
*/
add_action( 'init', 'tgm_soliloquy_restrict_admin_access', -1 );
function tgm_soliloquy_restrict_admin_access() {

    if ( ! is_admin() ) {
        return;
    }

    if ( class_exists( 'Soliloquy' ) ) {
        if ( ! current_user_can( 'update_core' ) ) {
            remove_action( 'init', array( Soliloquy::get_instance(), 'init' ), 0 );
            remove_action( 'widgets_init', array( Soliloquy::get_instance(), 'widget' ) );
        }
    }

}
#335863

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Thank you for contacting us here in the Support Forum

I am confused about your requirements:

I expected to see:
After implementing solution recommended on that link, the "Fields and Views" button still displays for non-admins who have the capability to add and publish new content/products.

Instead, I got:
The button is still there.

I understand from this, if you want the button to still be there, the expectations are met.

If you want to hide certain elements by role, you can use this if statement:
https://codex.wordpress.org/Function_Reference/current_user_can

current_user_can( $capability ); //allows for this certain Role or capability
! current_user_can( $capability ); // does NOT allow for this Role or capability

The code you use for Soliloquy is evaluating if the user is not a Admin:

! is_admin()

Therefore if you want the same rule for Toolset, you can change your code to using the check for "is not admin"

Please let me know if you have further questions regarding the issue mentioned in this Thread

Thank you for your patience.

#335918

I guess I can see why you might have gotten confused. The dropdowns on your support form asked which knowledge base link I'd reviewed and what I got instead. I presumed that meant I should tell you what didn't work about the method I tried. I expected the WP-Types button to not be there. Is that clearer? (smile)

In the Soliloquy function, the next statement evaluates whether or not the capabilities are there to update core, right? That was effective in acquiring what I was endeavoring to accomplish.

Here's more background. I'm developing a WordPress Marketplace running on WC Vendors which requires WooCommerce. WC Vendors introduced a custom role to the site of Vendor. Vendors are able to upload images and create products at the site. They can publish and publish products and they can upload images.

I don't want to confuse these non admin users with a button for WP types because it looks like they could create one. I don't want them to do this because (a) it would create a user support nightmare trying to explain HOW to use it and (b) they might create something that is completely incompatible with the site layouts that are styled already.

My goal is to have ONLY the upload media button there, which is all that I want Vendors to use.

#336030

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Thanks for the Details

I believe it doesn't work because the CSS class has changed.

See, I can successfully hide the Views Button for my (with Access) created Custom Role "custom" using this approach:

1. Create Custom Role (in my case level 3) "custom" with Access
2. Create a wptypes-special.css CSS file, in your Themes Folder
3. Add this CSS to the File:

span.button.wpv-shortcode-post-icon.js-wpv-shortcode-post-icon-wpv-views{ 
	display:none !important; 
}

For Views AND CRED:

span.button.wpv-shortcode-post-icon.js-wpv-shortcode-post-icon-wpv-views, span#cred-form-shortcode-button.cred-media-button{ 
	display:none !important; 
}

4. Put this conditional in your functions.php:

function vm_wptypes_remove() {
    if ( current_user_can( 'custom' ) ) {
        wp_enqueue_style( 'wptypes-special', get_stylesheet_directory_uri() . '/wptypes-special.css' );
    }
}
add_action( 'admin_init', 'vm_wptypes_remove' );

It hides the Views Button successfully for my custom role only. I think that is what you need, right?

Please don't hesitate to inform me in case the issue persists
and let me know if the above solution works for you, I look forward to your reply!

Thank you for your patience.

#336099

I didn't really want to add another plugin to this development so all that I needed was the new CSS classes. Perhaps, you ought to update the knowledgebase link with this information too.

Here is my final code:

Functions

add_action( 'admin_init', 'vm_wptypes_remove' );
function vm_wptypes_remove() {
    if ( ! current_user_can( 'update_core' ) ) {
        wp_enqueue_style( 'wptypes-special', get_stylesheet_directory_uri() . '/wptypes-special.css' );
    }
}

Contents of wptypes-special.css:

span.button.wpv-shortcode-post-icon.js-wpv-shortcode-post-icon-wpv-views { display:none !important; }

Thanks for your help on this. My problem is resolved.

#336100

Beda
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Great.

I did not mean to suggest a new plugin. My Example with Access just was because I use access for Custom Roles.
But it's the same approach, the important are the correct CSS Classes.

Those you can usually retrieve with a Browser Console like firefox has, which allows you to easy spot the correct class.

This might change (although it's not planned) in future development, so if this code "breaks" in future, the first thing to check would be the CSS.

Thank you for your patience