Search code examples
phpwordpressuser-roles

Wordpress - Hide specific fields to certain user role on back end


Is there a way in which I can disable/hide specific fields from the "page form" in the back-end? I have currently test two plugins but none of them meet the requirement that I have, like for example this plugin "User Role Editor by Members – Best User, Role and Capability Management Plugin for WordPress":

enter image description here

I'm sorry that the options are in spanish but the capabilities options are for example: "edit pages", "edit other people's pages", "publish pages", "delete pages"... What I want is to hide certain sections to the "editor" user role from the page form:

enter image description here

Is this even possible?, and if not, what alternatives do I have?, the thing is that I'm afraid that whoever is in charge of editing will end up messing up with the pages...


Solution

  • In most cases, the plugin "Adminimize" that Tech Sourav mentioned will work just fine, but since this page I'm working with is using some annoying custom content editor and other theme tools, the workaround that I ended up doing was:

    1. Create a custom post type with the "Custom Post Type UI" plugin.

    enter image description here

    1. In this case I will only and always have 3 pages that have this "modifiable" price field, so once the custom post type is added I added 3 entries to this custom post type (here I will show you just one).

    enter image description here

    1. I'm also using the "advanced custom fields pro" plugin in order to create this price field into my entries.

    enter image description here

    1. When the custom field has been created there's this option inside the same previous form that allows me to show that specific field just created into specific post types entries, so I made up the rules in order to show this field in the created custom post type entries:

    enter image description here

    In this example I'm just showing you the rule for one entry, in order to make it appear on the other 2 entries, I would just click in the "agregar grupo de reglas" and select one by one.

    1. Once this custom field and rules are configured correctly, I should see now the custom field "precio" inside my specified entries form:

    enter image description here

    1. After saving the entry with the updated custom field value, I will now check the post id and save it for later (we're going to need it):

    2. According to the documentation of Advanced Custom Fields plugin I can show the saved value of certain input within certain post with the shortcode: [acf field="{$field_name}" post_id="{$post_id}"], in this case I will change the values to: [acf field="precio" post_id="1372"] and paste the code into the section of the page that I will show this:

    enter image description here

    The builder that this page is using has an element called "code block" but you can use it in the equivalent element that allows you to paste code from your theme.

    1. After saving the changes, now the only thing that I have left is to restrict the ability to edit pages to a user with the "editor" role, for this I will use the plugin that I mentioned before in my question called "User Role Editor by Members – Best User, Role and Capability Management Plugin for WordPress":

    enter image description here

    As you can see, I will not let editor users to mess up with the pages post types, so with this, they will only be able to edit the custom post type that I created, which even if they write something inside the content of that custom post type entry, the website won't be affected at all. This may be a little bit hacky but it works...