Home Forums ACF PRO Dynamically add options


Dynamically add options

  • I’ve looked at the documentation to create dynamic fields here:

    Been playing around with it for awhile, but with no luck. Here’s what Im trying to do:

    1. I have a Repeater field named ‘slides’
    2. Within that repeater field, I gave a Group sub field named ‘templates’
    3. Within that sub field, I have a Group sub field named ‘default’
    4. Lastly, within the default sub field, I have a Radio field named ‘image_size’

    What I’m trying to is dynamically add the available WP image sizes in that field. I need help on customizing the default sub field options.

    Hope that all makes sense

  • It doesn’t matter in which level of the repeaters the field is placed.

    Try this code to get the image sizes as options for your radio field:

    function acf_load_image_sizes_field_choices( $field ) {
    	global $_wp_additional_image_sizes;
    	$field['choices'] = array();
    	foreach ( get_intermediate_image_sizes() as $size ) {
    		if ( in_array( $size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
    			$width  = get_option( "{$size}_size_w" );
    			$height = get_option( "{$size}_size_h" );
    			$crop = (bool) get_option( "{$size}_crop" );
    		} elseif ( isset( $_wp_additional_image_sizes[ $size ] ) ) {
    			$width = $_wp_additional_image_sizes[ $size ]['width'];
    			$height = $_wp_additional_image_sizes[ $size ]['height'];
    			$crop = $_wp_additional_image_sizes[ $size ]['crop'];
    		$image_crop = "";
    		if ( $crop == 1 ) {
    			$image_crop = " - cropped";
    		// set the value for field values and labels as you like
    		$value = $size;
    		$label = $size . " ({$width}x{$height}{$image_crop})";
    		$field['choices'][ $value ] = $label;
    	// add full size option
    	$field['choices'][ 'full' ] = "Full size";
    	return $field;
    add_filter('acf/load_field/name=image_size', 'acf_load_image_sizes_field_choices');

    This filter works for the field named image-size.

    I hope you get the idea how this works and you can adjust it to your needs.


  • Works great! Thanks a lot!

Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘Dynamically add options’ is closed to new replies.