Support

Account

Home Forums Add-ons Gallery Field Problem with use acf/format_value/ filter for gallery field Reply To: Problem with use acf/format_value/ filter for gallery field

  • Hope this helps you

    
    // add filter w/high priority so it runs after ACF
    add_filter('acf/format_value/name=gallery', 'add_values_to_gallery', 20, 3);
    function add_values_to_gallery($value, $post_id, $field) {
      if (!$value || !is_array($value) || !count($value)) {
        // no value, bail early
        return $value;
      }
      // loop through gallery
      // note & before $images => pass by reference
      // so that changes to $image effect original
      // http://php.net/manual/en/language.references.pass.php
      foreach ($value as $index => &$image) {
        // add a value to this image
        $image['my-new-index'] = 'my new value';
      }
      return $value;
    }