Support

Account

Home Forums Backend Issues (wp-admin) ACF load_value to Gallery Field

Solving

ACF load_value to Gallery Field

  • Hi there,

    I’m trying to find a way to load images from a gallery plugin (Royal Slider) to an ACF Gallery field. At present each post has a slider ID linked to it using an ACF load_field filter function:

    add_filter('acf/load_field/name=media_gallery_slider', 'my_acf_royalslider_choices');
    
    function my_acf_royalslider_choices($field){
    
      $field['choices'] = array();
    
      global $wpdb;
    
      $results = $wpdb->get_results('SELECT * FROM ' . $wpdb->prefix . 'new_royalsliders ORDER BY ID ASC');  
    
      if(!empty($results)) :
    
      foreach($results as $result) :
    
        $value = $result->id;
        $label = $result->name;
        $field['choices'][ $value ] = $label;
    
      endforeach;
      endif;
      return $field;
    
    }
    

    So I’d like to use the ID value from this function with a load_value filter to populate a gallery field but I’m unsure as to how to pass that value to use in the next function and how to build the array of image IDs?:

    add_filter('acf/load_value/key=field_5e2058d65e519', 'build_gallery_from_rs', 10, 3);
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
    
      // How to get the Slider ID of current post?
      $id = 123;
      $table = $wpdb->prefix . 'new_royalsliders';
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
    
      // How to build image array?
    
      //$media = ??;
      $value = array();
      if ($media) {
        foreach ($media as $image) {
          $value[] = $image->ID;
        }
      }
      return $value;
    }
    

    Any help would be great! Many thanks

  • To be honest, I’m not clear on what you’re trying to do, but hopefully correcting a couple of issues I see in your second code snippet and answering your questions there will help.

    first, at the top of your function you need to return the current value if set

    
      if (!empty($value)) {
        // already has a value
        return $value;
      }
    

    the question

    
    
      // How to get the Slider ID of current post?
      $id = 123;
      $table = $wpdb->prefix . 'new_royalsliders';
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
    
    

    should be something like

    
    $id = get_field('media_gallery_slider', $post_id, false);
    
  • Hi John,

    Many thanks for the response!

    What I’m trying to do is take the array of images from a posts slider Id so that I can populate the gallery field and eventually remove the royalslider plugin as a dependency. There are hundreds of posts so I didn’t want to have to create each gallery from scratch.

    Here’s an example of how the royalslider plugin stores an array in its ‘slides’ column in the database:

    
    [{"title":"Slider 47","image":{"attachment_id":"1653"}},{"title":"Image-1","image":{"attachment_id":"1652"}},{"title":"Image-2","image":{"attachment_id":"1651"}},{"title":"Image-3","image":{"attachment_id":"1650"}},{"title":"Image-4","image":{"attachment_id":"1649"}},{"title":"Image-5","image":{"attachment_id":"1648"}}]
    

    Using your ammendments I can now link to the correct slider ID but images are not being loaded into the gallery field:

    
    add_filter('acf/load_value/key=field_5e2058d65e519', 'build_gallery_from_rs', 10, 3);
    
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return $value;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
      $table = $wpdb->prefix . 'new_royalsliders';
      $id = get_field('media_gallery_slider', $post_id, false);
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
      // How to build image array?
      $media = $slider_data[0];
      $value = array();
      if ($media) {
        foreach ($media as $image) {
          $value[] = $image->ID;
        }
      }
      return $value;
    }
    
  • It appears that the slider stores the value in JSON.

    Something like this might work

    
    // in your filter after getting the value from the db
    $value = array();
    $slider = json_decode($slider_data, true);
    if ($slider) {
      foreach ($slder as $slide) {
        $value[] = $slide['image']['attachment_id'];
      }
    }
    return $value
    
  • This is giving a Warning: json_decode() expects parameter 1 to be string. If I print the result of $slider_data:

    
    Array ( 
      [0] => Array ( 
        [id] => 47 
        [active] => 1 
        [type] => custom 
        [name] => Slider Title 
        [skin] => myCustomSkin 
        [template] => simple_vertical 
        [slides] => [
          {
           "title":"Image 1",
           "image":{"attachment_id":"1653"}
          },
          {
            "title":"Image 2",
            "image":{"attachment_id":"1648"}
          }
        ] 
        [options] => {
          // A whole bunch of options
        } 
        [template_html] =>
          {{image_tag}} {{thumbnail}} {{html}} {{animated_blocks}} {{#link_url}} {{title}} {{/link_url}}
      ) 
    )
    

    Printing $value returns:

    
    Array ( )
    

    I only need what’s in the slides array. Does this change how the array gets traversed?

  • So, for some reason the json that is stored in the DB is being partially decoded, which is odd.

    This value in the DB is obviously a json srting

    
    [{"title":"Slider 47","image":{"attachment_id":"1653"}},{"title":"Image-1","image":{"attachment_id":"1652"}},{"title":"Image-2","image":{"attachment_id":"1651"}},{"title":"Image-3","image":{"attachment_id":"1650"}},{"title":"Image-4","image":{"attachment_id":"1649"}},{"title":"Image-5","image":{"attachment_id":"1648"}}]
    

    But I don’t know why your query would be returning the decoded value or why the value of the slides index would appear to still be a json value.

    You might want to try

    
    foreach ($slider_data as $slide) {
      $attachments = json_decode($slide['slides'], true);
      if ($attachments) {
        foreach ($attachments as $attachment) {
          $value = $attachment['attachment_id'];
        }
      }
    }
    
  • Hi John,

    So there’s no more warning, and if I print_r on attachments I get:

    
    Array ( [0] => Array ( [title] => Image 0 [image] => Array ( [attachment_id] => 1653 ) ) [1] => Array ( [title] => Image 1 [image] => Array ( [attachment_id] => 1652 ) ) [2] => Array ( [title] => Image 2 [image] => Array ( [attachment_id] => 1651 ) ) [3] => Array ( [title] => Image 3 [image] => Array ( [attachment_id] => 1650 ) ) [4] => Array ( [title] => Image 4 [image] => Array ( [attachment_id] => 1649 ) ) [5] => Array ( [title] => Image 5 [image] => Array ( [attachment_id] => 1648 ) ) )
    

    It looks like the correct data is being returned but there are still no images loading in gallery field. Just checking that I’ve understood you correctly, this is the ammended snippet:

    
    function build_gallery_from_rs($value, $post_id, $field) {
      if (!empty($value)) {
        // already has a value
        return $value;
      }
      // does not have a value, build value from slider attachments
      global $wpdb;
      $table = $wpdb->prefix . 'new_royalsliders';
      $id = get_field('media_gallery_slider', $post_id, false);
      $slider_data = $wpdb->get_results( $wpdb->prepare("SELECT * FROM $table WHERE id=%d", $id), ARRAY_A );
      
      // Build image array
      //$value = array();
      if ($slider_data) {
    
        foreach ($slider_data as $slide) {
          $attachments = json_decode($slide['slides'], true);
          //print_r($attachments);
          if ($attachments) {
            foreach ($attachments as $attachment) {
              $value = $attachment['attachment_id'];
            }
          }
        }
    
      }
      return($value);
      
    }
    

    Many thanks again!

  • You need to figure out how to loop over the array created by this $attachments = json_decode($slide['slides'], true); to extract the image post ID for each of the images. I can’t tell by looking at what you posted for the output of that array.

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

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.