Home › Forums › General Issues › Country Flags (graphics/icons) with ACF? › Reply To: Country Flags (graphics/icons) with ACF?
Hello there,
Thank you for this. I was searching something similar.
As I work with a child theme, the code had to be modified and also needed some corrections. Here is a code working for a child theme :
add_filter('acf/prepare_field/name=slug_field', 'populate_flag_field', 20);
function populate_flag_field($field) {
$choices = array();
$dir = get_stylesheet_directory() . '/flags';
$files = scandir($dir);
if ($files) {
foreach ($files as $file) {
if (is_dir($dir . '/' . $file)) {
continue;
}
$file_info = pathinfo($file);
if ($file_info['extension'] != 'png') {
continue;
}
$label = $file_info['filename'];
$choices[$file] = $label;
}
}
$field['choices'] = $choices;
return $field;
}
add_filter('acf/format_value/name=slug_field', 'format_flag_field', 20, 3);
function format_flag_field($value, $post_id, $field) {
if ($value) {
$value = '<img src="' . get_stylesheet_directory_uri() . '/flags/' . $value . '" />';
}
return $value;
}
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 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 Privacy Policy. If you continue to use this site, you consent to our use of cookies.