Support

Account

Home Forums Bug Reports ACF Pro – Options pages with Repeaters not working

Solving

ACF Pro – Options pages with Repeaters not working

  • I’m not entirely sure what you mean by “without giving too much away”. I’m not the developer of ACF, I just help out here on the forums and try to solve problems. Like you I’m just a user, I would need to dig around in the ACF code myself to figure it out.

    That said, I have attempted to recreate what you’re seeing without any luck. I have create field groups on posts and options pages, the same groups and different groups. I even set up two groups on two pages and used the same field name for the repeater. Even in this case I still got an array back, the problem that is cause was that the values from one options page would overwrite the values saved on a the other options pages, so it didn’t cause any problem with what was returned.

    Generally, where this has happened in the past it has been my experience that it’s generally caused by some other plugin or some filter being run that is somehow interfering with ACF. If I could duplicated this error consistently on a test site I would report it as a bug to the developer.

    If you’d like, I can test your the field groups that are not returning the correct value on a test site where I’m running a bare WP installation with an unaltered default theme and only ACF.

  • Sorry John, I misinterpreted your role (thought you worked for the developer). I’ll look further and post back if I get any further insights. My plugin is interfering in some way, but I don’t really understand what is happening…

    Cheers, David

  • I do work for the developer, but in the limited capacity of helping people here on this forum.

    If you are attempting to build some type of plugin that works with ACF, there are a couple of things to keep in mind, and that information helps. I don’t recall you saying you were adding this to a plugin but I could have missed it.

    When adding field groups you need to add them differently than when adding them in a theme. In the theme you can just check to see if the function exists, using the exported code. When adding them in the plugin you need to add them on the acf hook acf/include_fields or on the WP init hook. You can probably also use the WP plugins_loaded hook. More then likely if you’re just checking for the function and your plugin loads before ACF loads then the field group is not being created because the function does not exist… yet.

    If you are getting existing fields or field groups and attempting to modify them you can run into trouble as well. ACF stores the field groups and fields in a cache and if you modify them after they have been cached then those changes will not be effective unless you clear the ACF cached value. If this is the case then you’ll want to dig into the ACF functions that you’re calling to get the fields and groups, figure out what cache value is being set and then clear that value before trying to make your changes.

  • Hi John

    Thanks for this. I do use the plugins_loaded hook, but there could be paths through my code where this happens too late (my plugin is supposed to run in certain circumstances without needing to fire up WordPress fully.

    Thanks for the initial hint – could be what I need to find the solution.

    Thanks, David

  • If you’re trying to circumvent needing WP to load completely then you might want to take look at accessing the repeater field directly using get_option() instead of get_field(). To be 100% honest, I almost never use get_field() or most of the other functions built into ACF, basically because I want everything to work should ACF be deactivated for some reason…. of course if this is more than simple fields, like images or wysiwyg editors then it can be a lot more work. Here’s an example.

    
    // the options name used by ACF to store the repeater
    $repeater = 'options_'.$your_repeater_field_name;
    // array of sub field names
    $subfields = array(
      'field_1' => 'text',
      'field_2' => 'image',
      'field_3 => 'wysywig'
    );
    // place to put the repeater values
    $values = array();
    $count = intval(get_option($repeater, 0));
    for ($i=0; $i<$count, $i++) {
      $value[] = array();
      foreach ($subfields as $subfield => $type) {
        $value[$subfield] = get_option($repeater.'_'.$i.'_'.$subfield, '');
        switch($type) {
          case 'image':
            $value[$subfield] = wp_get_attachment_image_src(intval($value[$subfield]), 'full');
            break;
          case 'wysiwyg':
            $value[$subfield] = apply_filters('the_content', $value[$subfield]);
            break;
          case 'text';
          default:
            // do nothing;
        }
      } // end foreach $subfield
      $values[] = $value;
    } // end for $i
    
  • Hi John and others

    I’ve finally tracked down what was causing my problem with the repeater field. There were circumstances when I was calling ACF code too early in the overall initialisation process. I moved some of my initialisation to be triggered later (fired on WordPress init) and the problem is solved.

    Hopefully this will help others

  • Hey David,

    Thanks for sharing your solution on the repeating field issue. I should have looked at priority more. Cheers.

  • Thanks John, I think that’s the route I’ll have to go since I’m also calling fields during a schedule task.

  • In my case John’s solution of using get_option or get_post_meta was the best solution. It’s not quite as convenient, but I was loading field values for a scheduled job in WordPress so timing wasn’t an option for me. Hopefully this thread is able to help others.

Viewing 9 posts - 26 through 34 (of 34 total)

The topic ‘ACF Pro – Options pages with Repeaters not working’ is closed to new replies.