Skip Navigation

[Resuelto] wp-admin: How to hide certain custom field inputs based on post ID

This support ticket is created hace 4 años, 11 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.

Hoy no hay técnicos de soporte disponibles en el foro Juego de herramientas. Siéntase libre de enviar sus tiques y les daremos trámite tan pronto como estemos disponibles en línea. Gracias por su comprensión.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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: Asia/Kolkata (GMT+05:30)

Este tema contiene 7 respuestas, tiene 2 mensajes.

Última actualización por Minesh hace 4 años, 11 meses.

Asistido por: Minesh.

Autor
Mensajes
#1431873

Is there a PHP function to hide certain custom fields based on the post currently being edited?

#1432305

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please clarify where exactly you want to hide the custom fields? Do you want to hide in the backend admin while editing the post? If yes:

Please check the following Doc:
=> https://toolset.com/documentation/user-guides/custom-content/types-custom-fields-conditional-display/

if no:
Please elaborate more if that is not the case

#1432993
Screen Shot 2019-12-31 at 11.08.24 AM.png

How can I limit certain fields to only Administrators? I only see the conditional options for one field conditional upon another field.

#1433805

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

If you want to show/hide the custom field based on the role than you need to add some custom code to your theme's functions.php file.

- You need to make sure that the custom fields you want to show/hide based on the role should be part of the custom field group. That means you should have a custom field group where you should add all those custom fields.
- Add the following code to your current theme's functions.php file:

function func_remove_specific_custom_fields() {
    global $post_type;
    global $post;
     global $current_user;
 
if(!in_array('administrator',(array)$current_user->roles) and $post_type=='your-post-type'){
 
            remove_meta_box( 'wpcf-group-{YOUR-POST-FIELDGROUP-SLUG}', $post_type,'normal');
}
}
add_action('admin_head', 'func_remove_specific_custom_fields' );

Where:
- Replace '{YOUR-POST-FIELDGROUP-SLUG}' with your original custom fields group slug.
- Replace 'your-post-type' with your posttype slug where you want to apply the above code.

Please feel free to adjust the above code if required and I hope the above solution will help you to resolve your issue.

More info:
=> https://codex.wordpress.org/Function_Reference/remove_meta_box

#1436037

Thanks for that, but I'm wanting to remove via PHP a single Field within a Field Group.

#1436043

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

To remove the field group, there is no such option available or needs custom CSS to remove it.
or
you should create a custom field group, move the field you wanted to that field group and use the code I shared.

#1436693

Sounds like a reasonable workaround, but I'd love to see PHP be able to filter and Access to have this level of control -- maybe add an "Advanced" or "Detailed" or "Fine Grained" settings section as a feature request.

Anyway... how do I move a single Field from one Field Group to another Field Group?

#1437453

Minesh
Supporter

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Sounds like a reasonable workaround, but I'd love to see PHP be able to filter and Access to have this level of control -- maybe add an "Advanced" or "Detailed" or "Fine Grained" settings section as a feature request.
==>
Yes - Please feel free to file a feature request using the following link:
- https://toolset.com/home/contact-us/suggest-a-new-feature-for-toolset/

Anyway... how do I move a single Field from one Field Group to another Field Group?
==>
You can create a new custom field group and add try to add the field. If you do not know how to do it, Please share admin access details and what field you want to move and I would be happy to assist.

*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.

I have set the next reply to private which means only you and I have access to it.