How to populate ACF Gallery with RoyalSlider Images

marco's Avatar

marco

14 Jan, 2020 05:13 PM

Hi there,

I'm using the Royalslider WP plugin but there are some pages on my site where I need to implement a basic gallery. I'm already using ACF for lots of other fields and wondered if it was possible to get images from an RS gallery into an ACF gallery without having to do it manually? The images need to populate the ACF field in the backend - not just front.

I don't embed the RS shortcode into the editor. Instead I store an array of sliders in an ACF select field and then use the load_field filter like so:

<?php 
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;
}
?>

Then in a template file I build the shortcode:

<?php 
$sliderID = get_field('media_gallery_slider'); 

    if ($sliderID == '')
    {
        
    }
    else { 
      echo do_shortcode( '[new_royalslider id="' . $sliderID . '"]'); 
    }
?>

So I'm wondering if it's possible to use $sliderID to get the array of images and load into the ACF gallery?

Any help would be awesome!

Cheers

  1. Support Staff 1 Posted by Dmitry Semenov on 14 Jan, 2020 08:29 PM

    Dmitry Semenov's Avatar

    Hello,

    The plugin does not have built-in api for such functionality, but you can query db directly the same way as you do in your first example, e.g.:

    global $wpdb;
    $id = 123; // slider id
    $table = $wpdb->prefix . 'new_royalsliders';
    $slider_data = $wpdb->get_results( $wpdb->prepare( 
                "
                    SELECT * FROM $table WHERE id=%d
                ", 
                $id
            ), ARRAY_A );
    
  2. 2 Posted by marco on 16 Jan, 2020 02:26 PM

    marco's Avatar

    Hi Dmitry

     

    In this example would I need to keep editing the slider ID? Ideally I’d want to target the slider ID that’s been used in the current post

     

    Also, does $slider_data load just the array of attachment IDs with ARRAY_A or do I need to get these from the slides column and use get_attached_media?

     

    Apologies for all the questions

     

     

    Marco Terrinoni - Director
    4 Divisions
    web design / app development / digital marketing
    uk: +44 7876 652 643

  3. Support Staff 3 Posted by Dmitry Semenov on 16 Jan, 2020 05:19 PM

    Dmitry Semenov's Avatar

    In this example would I need to keep editing the slider ID?

    It's up to you, aren't you storing slider ID in some custom field? referring to this code from your post:

    $sliderID = get_field('media_gallery_slider');
    

    Also, does $slider_data load just the array of attachment IDs with ARRAY_A or do I need to get these from the slides column and use get_attached_media?

    Depending on type of slider that you're using data is managed slightly differently. You may print $slider_data that you query to see its data structure, as it's not just attachment ids.

  4. 4 Posted by marco on 16 Jan, 2020 06:13 PM

    marco's Avatar

    $sliderID = get_field('media_gallery_slider');

    Jesus I’m dumb J

     

    Ok, so the sliders are custom and in this is an example of an array from the slides column:

     

    [{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Suite-Executive-Governor-Suite","image":{"attachment_id":"1653"}},{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Suite","image":{"attachment_id":"1652"}},{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Room","image":{"attachment_id":"1651"}},{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Restaurant","image":{"attachment_id":"1650"}},{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Lounge","image":{"attachment_id":"1649"}},{"title":"Argentina-Holiday-Tours-Alvear-Palace-Interior-Buffet","image":{"attachment_id":"1648"}}]

     

     

     

     

    Marco Terrinoni - Director
    4 Divisions
    web design / app development / digital marketing
    uk: +44 7876 652 643

Reply to this discussion

Internal reply

Formatting help / Preview (switch to plain text) No formatting (switch to Markdown)

Attaching KB article:

»

Attached Files

You can attach files up to 10MB

If you don't have an account yet, we need to confirm you're human and not a machine trying to post spam.

Keyboard shortcuts

Generic

? Show this help
ESC Blurs the current field

Comment Form

r Focus the comment reply box
^ + ↩ Submit the comment

You can use Command ⌘ instead of Control ^ on Mac