Skip to content

Instantly share code, notes, and snippets.

@mrpsiho
Created June 10, 2024 13:03
Show Gist options
  • Save mrpsiho/81b57e3c1265fa653678c6be89a5faab to your computer and use it in GitHub Desktop.
Save mrpsiho/81b57e3c1265fa653678c6be89a5faab to your computer and use it in GitHub Desktop.
Example of dynamically populated choices for ACF select field
add_filter( 'acf/load_field', 'my_plugin_get_proper_form_templates', 10, 1 );
function my_plugin_get_proper_form_templates($field) {
if ( 'select_form_tmpl' === $field['name'] ) {
$forms_cfg = my_plugin_get_form_fields_cfg(); // external function, we get data from here
$choices = [];
if (array_keys($forms_cfg)) {
foreach (array_keys($forms_cfg) as $tmpl_name) {
$choices[$tmpl_name] = $forms_cfg[$tmpl_name]['label'];
}
}
$field['choices'] = $choices;
}
return $field;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment