Home › Forums › Front-end Issues › External condition for displaying field
Hey,
I was wondering whether it’s possible to show a field basend on condition (apart from the built in conditional logic).
Something like hooking into the field and if a certain condition is not met not to display the field.
I hooked into acf/load_field/name=my_field
and in my function used
public function my_acf_load_field($field)
{
if(!my condition):
$field = null;
return $field;
endif;
}
But this still renders an empty text input (instead of the repeater field) on my frontend form. Is there a way to not render/display the field at all?
Thanks,
Florian
If anyone is able to help, this would be highly appreciated. Thank you.
Is this something you want to have change, like the current conditional logic makes fields appear and disappear or something that will just hide or show the field and it will not change if other selections are made?
I want to show / hide the field in the frontend form based on a custom user meta.
So no conditional relationship to any other fields. I want to have some sort of php condition whether to show the field for the specific user at all in the frontend form (where users can send in posts).
Something like this (pseudo code)
if(get_user_meta('myfield') == 'myvalue'):
show_this_acf_repeater_field;
endif;
I was about to post that you could set the class of wrapper to hidden
and then add some custom CSS when I stumbled upon something that I was unaware of.
Using and acf/load_field filter http://www.advancedcustomfields.com/resources/acfload_field/
add_filter('acf/load_field/name=test_hidden_field', 'load_field_test_hidden_field');
function load_field_test_hidden_field($field) {
$condition = false;
if (!$condition) {
$field['wrapper']['class'] = 'hidden';
}
return $field;
}
Thanks a lot. This works. But I’m a bit concerned that the field is still part of the form (just hidden)… for security reasons I’d rather see a possibility to use the load field filter in a way to remove the field. But this seems not to be possible.
No, I don’t think it’s possible, at least not using filters.
It would be possible if you dynamically generated and registered the field group in code. Doing this you could check for your condition and if it’s not met then you don’t add the field to the group. But that’s about the only way I can think of to do what you want.
Thank you for your input. I tried this last night but it didn’t work
I tried to conditionally register a repeater field that should sit within a tab field… but I threw out some errors.
I think I stick to your class solution. Thanks a lot!
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.