StoutLogic / acf-builder

An Advanced Custom Field Configuration Builder
GNU General Public License v2.0
792 stars 61 forks source link

Image Selector #155

Closed erralb closed 2 years ago

erralb commented 2 years ago

Hi there, how can you achieve an Image Selector field ?

https://www.acf-extended.com/features/fields/image-selector

stevep commented 2 years ago

There is a plain addField function. First parameter is the field name, the second is the field type, and the third is the array of arguments for field settings. You'll have to find out what the field type name is from the acfe library, but I believe it's acfe_image_selector.

So you could use the following:

$group->addField('my_field_name', 'acfe_image_selector', []);

I thought the addField function was documented in the wiki, but it appears not to be the case.

erralb commented 2 years ago

Thanks, I will try this and post a working example !

erralb commented 2 years ago

So in the end I used https://github.com/cyberwani/ACF-Image-Select instead of the ACF extended plugin.

To add the field :

            ->addField('zindex','image_select', [
                'image_path' => get_stylesheet_directory_uri().'/resources/images/zindex/',
                'choices' => [
                    '1' => 'z-index 18',
                    '2' => 'z-index 12',
                    '3' => 'z-index 14',
                    '4' => 'z-index 16',
                ]
            ])->setDefaultValue('1')

The images must be named 1.png, 2.png, 3.png and 4.png for this to work.

I also modified the ACF-Image-Select plugin in order to get the values instead of the images' path with the get_field() method, in a similar fashion as explained in this issue.