Exclude fields based on label

By: Peter S | Asked: 05/09/2024
ForumsCategory: Code HelpExclude fields based on label
Peter S asked 7 months ago

Hey, 

Does anyone know how to adapt this code so instead of excluding fields based on their ID, it excludes them based on their label (name)? 

add_filter( 'frm_entry_values_exclude_fields', 'specify_exclude_fields', 10, 2 );

function specify_exclude_fields( $field_ids, $atts ) {

$field_ids[] = 3013; return $field_ids;

}  

Here is the link to the code:
https://formidableforms.com/knowledgebase/frm_entry_values_exclude_fields/   Thank you!

3 Answers
Best Answer
Peter S answered 7 months ago
Solved it with this code:
Start your code here
add_filter('frm_entry_values_exclude_fields', 'specify_exclude_fields', 10, 2); function specify_exclude_fields($field_ids, $atts) { // Hard-coded form ID $form_id = 78; // Replace 78 with the actual form ID // Define a list of field labels to exclude $exclude_labels = ['Label 1', 'Label 2', 'Label 3']; // Replace these with actual labels to exclude // Fetch all fields for the form $fields = FrmField::get_all_for_form($form_id, '', 'include'); // Loop over each field and check the label foreach ($fields as $field) { if (in_array($field->name, $exclude_labels)) { // If the label is in the exclusion list, add its ID to the $field_ids array $field_ids[] = $field->id; } } return $field_ids; }
Rob LeVineRob LeVine answered 7 months ago
You can loop over the field_ids array, use FrmField::getOne() on it and check the name. Then fill the $field_ids array with only the fields you want or remove from it the ones you don't want.
Peter S replied 7 months ago

Hey,
Thanks for the advice.

I tried using the following code, but can't get it working. Do you know how it to can improved?

add_filter( 'frm_entry_values_exclude_fields', 'specify_exclude_fields', 10, 2 );

function specify_exclude_fields( $field_ids, $atts ) {
// Assume we have a list of field labels to exclude
$exclude_labels = ['Exclude Label 1', 'Exclude Label 2']; // Replace these with actual labels to exclude

// Temporary array to store field IDs that match the exclusion criteria
$fields_to_exclude = [];

// Loop over existing field IDs and check each field's label
foreach ( $field_ids as $field_id ) {
// Get the field object
$field = FrmField::getOne( $field_id );

// Check if the field's label is in the list of labels to exclude
if ( in_array( $field->name, $exclude_labels ) ) {
// If it matches, add this field ID to the fields to exclude
$fields_to_exclude[] = $field_id;
}
}

// Filter out the field IDs that match the exclusion labels
$field_ids = array_diff( $field_ids, $fields_to_exclude );

return $field_ids;
}

Rob LeVineRob LeVine replied 7 months ago

I don't see anything glaringly obvious and it'll be next to impossible for me to debug in absentia so I suggest setting up WP debugging and log the values in your program and it'll eventually become clear. https://wpforms.com/developers/how-to-enable-debugging-in-wordpress/

Victor Font answered 7 months ago
You're using the filter incorrectly. When I tested this filter, it fires only when I view an entry in the admin area. The $field_ids parameter is empty. You add a field ID to exclude it from the view. In your code the $field = FrmField::getOne( $field_id ); will never fire because there are no values to trigger the foreach ( $field_ids as $field_id ) loop. What exactly are you trying to do? Where do you want to exclude fields?
Peter S replied 7 months ago

Hi,

I have lots of fields that I want to exclude from entries and they all share the same label. So instead of always having to add the field ID, I want to be able to add just the field label and therefore exclude all these fields automatically.

Thanks for the help!

Making the Best WordPress Plugin even better - Together

Take on bigger projects with confidence knowing you have access to an entire community of Formidable Experts and Professionals who have your back when the going gets tough. You got this!
Join the community
crossarrow-right