Skip Navigation

[Resolved] Custom post type edit form – certain fields can only be edited by admin

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

Problem:
The user would like to hide a field on a form for all user roles except administrators.

Solution:
Idealy, the user should create two separate forms, one for the administrators and the other for the user roles.

But the user wanted to use only one form and hide the field using CSS.

Check the suggested solution here https://toolset.com/forums/topic/custom-post-type-edit-form-certain-fields-can-only-be-edited-by-admin/#post-1945389

This support ticket is created 3 years, 11 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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 - - 9:00 – 13:00
14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 - - 14:00 – 18:00

Supporter timezone: Africa/Casablanca (GMT+01:00)

This topic contains 6 replies, has 2 voices.

Last updated by robertF-7 3 years, 11 months ago.

Assisted by: Jamal.

Author
Posts
#1943105

Tell us what you are trying to do?

I have a custom user role = "members"

I have a custom post type called "member profile" which contains about 20 custom fields

"members" can have multiple "profiles"

The member profile post type is updated by members and it has a custom ratings field.

I want that one field to be edited by admin only - not by the member.

All the other custom fields can be edited by the member.

I have designed an edit form and do not know how to hide the ratings field from the member.

The form is accessed from the custom template for the post type "member profiles"

Any help would be appreciated

#1944069

Hello and thank you for contacting the Toolset support.

If you want to let admins edit this field in the frontend from Toolset Forms, you will need to create a separate form for admins and add the field to it. Remove the field from the form that members use.

If on the other hand, you want to let only admins edit this field from the backend, you need to use the Toolset Access plugin and configure this field to be edited only by admins. Go to Toolset->Access Control->Types Fields(tab) and restrict the field for the other user roles.

I hope this helps. Let me know if you have any questions.

#1944117

thanks for the quick reply ... i understand that you could do it with a second form for admins

i would really like to achieve this with one form - i am new Toolset user and love the plugins so far - this is the first hurdle i have hit.

do you think this could be achieved using the more advanced feature like the HOOKS, CSS and JAVASCRIPT

is there a way to check if the user role is not an admin and hide the ratings field - just for editing a record, not for adding a new record - if the field is hidden with CSS would the form still work?

#1944135

I believe that it would be possible to hide the field using CSS. But for more security, you will also need a hook into the form validation to check if the user did not change the value of the field using the browser developer tools. Does it make sense?
The validation hook is cred_form_validate
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

You may want to add the user's role to the body classes using the body_class hook and target the field using CSS.
https://developer.wordpress.org/reference/hooks/body_class/

#1944523

thanks again for the quick reply.

i would like to try to just hide the field using css - do you have any code samples to point me in the right direction?

if the member (custom role=member) opens the form - the code would hide the field

if the admin opens the form they would see the field

#1945389

I run a small test where the field is always hidden unless the user is an administrator. So, that's different a bit from what you are looking for(hide the field for members), because it is hidden for anyone who's not an administrator. But that can explain the idea.

First, we need a way to know, from a CSS perspective, what's the current user role. For that, we need some custom code. I used the following code in a Toolset->Settings->Custom Code snippet. ** Please make sure that the snippet is active. ** Check this screenshot hidden link

add_filter('body_class', function($classes) {
    global $current_user;
    
    foreach ($current_user->roles as $user_role) {
        $classes[] = 'role-'. $user_role;
    }

    return $classes;
});

Then, you need to switch the form editor to the expert mode in order to add a CSS class(hide-field) to the field and target it with CSS. Check this screenshot hidden link
You can use any class name as long as you also use it in the CSS code.
Then, add a CSS code to implement the show/hide feature in the form. You can see where it has to be added in the previous screenshot.

.hide-field {
  display: none;
}

body.role-administrator .hide-field {
  display: block;
}

I hope this explains the how. Let me know if you have any questions.

#1945931

My issue is resolved now. Thank you!