Support

Account

Forum Replies Created

  • 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;
    }
    
Viewing 1 post (of 1 total)