Support

Account

Home Forums ACF PRO Can't retrieve repeater values from options page

Solving

Can't retrieve repeater values from options page

  • I have an options page with a repeater where I define some standard colors (the fields for this options page are created with PHP). Also I use the ACF RGBA Color Picker. This plugin has a filter to set the palette colors for the color picker and I added the correct code to the functions.php:

    <?php
    function set_acf_rgba_color_picker_palette() {
        // optional - add colors which are not set in the options page
        $palette = array(
            '#FFF',
            '#000'
        );
    
        if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
            while( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) { the_row();
                $palette[] = get_sub_field('MY_COLOR_FIELD');
            }
        }
    
        return $palette;
    }
    add_filter('acf/rgba_color_picker/palette', 'set_acf_rgba_color_picker_palette');
    ?>

    But the filter doesn’t get the color fields from the repeater. Only the the two colors defined in the palette array on top are returned. This shows me, that the filter works.

    My first thought was that I had a mistake in the field names. But for a radio field I dynamically populate the field’s choices from the same repeater of the options page with this function:

    function acf_load_color_field_choices( $field ) {
    	$field['choices'] = array();
    	if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
    		while ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) { the_row();
    			$value = get_sub_field('MY_COLOR_FIELD');
    			$label = get_sub_field('MY_LABEL_FIELD');
    			$field['choices'][ $value ] = $label;
    		}
    	}
    	return $field;
    }
    add_filter('acf/load_field/name=MY_RADIO_FIELD_NAME', 'acf_load_color_field_choices');

    This works as expected.

    My next thought was that I can’t get the options page for whatever reason. So I try to get another field from the options page which is not in a repeater:

    <?php
    function set_acf_rgba_color_picker_palette() {
        // optional - add colors which are not set in the options page
        $palette = array(
            '#FFF',
            '#000'
        );
    
        // ====================================================================
        // Try to get another field from MY_OPTIONS_PAGE
        // ====================================================================
        $test = get_field('MY_FIELD', 'MY_OPTIONS_PAGE');
        echo "<pre>";
        print_r($test);
        echo "</pre>";
    
        if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
            while( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) { the_row();
                $palette[] = get_sub_field('MY_COLOR_FIELD');
            }
        }
    
        return $palette;
    }
    add_filter('acf/rgba_color_picker/palette', 'set_acf_rgba_color_picker_palette');
    ?>

    The field that is not in a repeater is loaded and printed. So I’m save to get the options page.

    But why do I get no values from the repeater?

  • You code has me a bit confused

    
     if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
    

    Are you trying to use the name or slug of the options page? If you are then this is incorrect. When getting values for options pages the $post_id value is always 'option' or you can use 'options'

    
     if ( have_rows('MY_REPEATER', 'option') ) {
    
  • I think you are wrong. According to the documentation for the acf_add_options_page() the post ID can also be any string – only the default ist 'options'.

    $args = array(
    ...
    /* (int|string) The '$post_id' to save/load data to/from. Can be set to a numeric post ID (123), or a string ('user_2'). 
    	Defaults to 'options'. Added in v5.2.7 */
    	'post_id' => 'options',
    ...
    )
    

    Since I also get the values in other situations (see the 2nd example where I get all color values for the radio field or the 3rd example where I get the value for the other field from the same options page), the code can not be so wrong.

    Since the 'post-id' was introduced in v5.2.7 I always use this.

    Sorry, but this is not the solution…

  • I was curious, so I created an options page using the following

    
    $args = array(
      'title' => 'MY_OPTIONS_PAGE',
      'menu_title' => 'MY_OPTIONS_PAGE',
      'menu_title' => 'MY_OPTIONS_PAGE',
      'post_id' => 'MY_OPTIONS_PAGE'
    );
    

    Then I create a test field group with just a text field named “where_am_i”. I saved a value to it and then search the db for the value I save. It turned up in the options table with an option name of MY_OPTIONS_PAGE_where_am_i. I also tried some other field examples including a repeater field and they all end up in the options table with the prefix of MY_OPTIONS_PAGE_ and everything worked as expected, although I’m not exactly sure how because for me this is unexpected behavior (but intriguing since this opens up things I was not aware of)

    Anyway, this leads to the question of, if it does work, why isn’t it working for you.

    The only answer that I can come up with here is that you have another field that also evaluates to the option name of MY_OPTIONS_PAGE_MY_REPEATER... or that somehow your filter is interfering with the working of your have_rows() loop, though I can’t say why. When I run the code for the options in my page template on the site everything is working.

    For your last example try something like

    
    <?php
    function set_acf_rgba_color_picker_palette() {
        // optional - add colors which are not set in the options page
        $palette = array(
            '#FFF',
            '#000'
        );
    
        // ====================================================================
        // Try to the repeater as an array
        // ====================================================================
        $test = get_field('MY_REPEATER', 'MY_OPTIONS_PAGE');
        echo "<pre>";
        print_r($test);
        echo "</pre>";
    
        if ( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) {
            while( have_rows('MY_REPEATER', 'MY_OPTIONS_PAGE') ) { the_row();
                $palette[] = get_sub_field('MY_COLOR_FIELD');
            }
        }
    
        return $palette;
    }
    add_filter('acf/rgba_color_picker/palette', 'set_acf_rgba_color_picker_palette');
    ?>
    
  • I did some more digging and it seems that if “MY_OPTIONS” evaluates to a valid taxonomy name that your fields might be save incorrectly.

    When ACF tries to figure out where it should be saving a blue it explodes the post ID resulting in

    
    array(
      'MY',
      'OPTION',
      'PAGE'
    )
    

    then it combines all but the last value of the array and looks to see if that is a valid taxonomy.

    Anyway, I’d do a search of my database to see exactly where the values are being saved, if they are being saved at all.

  • John, thanks for your testing.

    I have checked my database and also checked if I have double field names (because I created the fields via PHP). No errors, no doubles.

    To be sure, I deleted all entries from the database belonging to my options page (no worries, I’m in a testing environment).

    After that, I added two new colors to my repeater and checked the database again. Everything is fine.

    But I was surprised when I tried to read and print the repeater field this way

    <?php
        $test = get_field('MY_REPEATER', 'MY_OPTIONS_PAGE');
        echo "<pre>";
        print_r($test);
        echo "</pre>";
    ?>

    I only get the number 2, the count of repeater rows.

    Then I create a repeater with fields in the backend (not via PHP) and tested this. And yes, it works!!!

    I exported this field groups to get the code and copy this into my functions.php to create the field groups via PHP to test if I make any mistakes on my first try.

    And that’s the next surprise: Now it does not work!!!

    With the above test I only get the number of the repeater rows and no array.

    So it must have to do anything with registering the field groups via PHP.

  • I figured out what’s going wrong:

    In the ACF RGBA Color Picker the apply_filters( "acf/acfrb_color_picker/palette", true ) in set in the __construct() of the field class in the $this->settings = array(). This setting is later used in a wp_localize_script() function in the function input_admin_enqueue_scripts()

    That seems to be to early. I moved the apply_filters... directly in the localized script part and now it works.

    Maybe you get an idea why this is working if setting the fields with ACF field groups page and not working if fields created via PHP.

    Thanks again for your help!

  • I think that the main issue may be that you’re attempting to get the repeater field before something is ACF is completely initialized. That’s the reason that you’re getting a number value for the repeater field rather than the array. ACF is returning the number of rows in the repeater, which is what the actual repeater value in the DB is. At least that’s my guess at the issue.

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

The topic ‘Can't retrieve repeater values from options page’ is closed to new replies.