musclehustle
musclehustle

Reputation: 33

Fetching revolution slider shortcodes

I'm trying something new here. I have a Wordpress custom theme. I have Advanced custom fields all setup. I'm using the advanced custom field's load_field function on a "select" field with "choices". What I want to basically do is to show the revolution slider shortcodes as choices for this field... This is my code in the functions.php file. Any help would be highly appreciated! :)

<?php

function my_acf_load_field( $field ) {

    $field['choices'] = array(
        <-- WANT REVOLUTION SLIDER SHORTCODES HERE -->
    );

    return $field;

}


// all
// add_filter('acf/load_field', 'my_acf_load_field');

// type
add_filter('acf/load_field/type=select', 'my_acf_load_field');

// name
// add_filter('acf/load_field/name=my_select', 'my_acf_load_field');

// key
// add_filter('acf/load_field/key=field_508a263b40457', 'my_acf_load_field');

?>

Upvotes: 2

Views: 5320

Answers (3)

AuRise
AuRise

Reputation: 2452

Answer updated April 22, 2020: For Slider Revolution V6, the function getAllSliderAliases() has been replaced with get_sliders() and returns an array of objects instead of an array of strings. Source.

functions.php for Slider Revolution V6

function my_acf_load_field( $field ) {
    if ( class_exists( 'RevSlider' ) ) {
        $rev_slider = new RevSlider();
        $sliders = $rev_slider->get_sliders();
        if(count($sliders) > 0) {
            foreach($sliders as $slider)
            {
                $field['choices'][$slider->alias] = $slider->title;
            }
        } else {
            $field['choices'] = array( 'none' => 'No sliders exist. You must create one first.' );
        }
    } else {
        $field['choices'] = array( 'none' => 'Slider Revolution plugin was not found.' );
    }
    return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

And then on the template or whatever page you're using the custom field on, I placed the actual shortcode there instead.

page.php

$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
    echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}

Old Answer for versions of Slider Revolution before V6

For whatever reason, my $sliders variable from Andrew M's answer wasn't returning anything, so I came up with this solution instead based on Themepunch's documentation for displaying any slider at random and this article for checking if the class exists first to avoid errors.

functions.php for Slider Revolution versions before V6

function my_acf_load_field( $field ) {
    if ( class_exists( 'RevSlider' ) ) {
        $rev_slider = new RevSlider();
        $slider_aliases = $rev_slider->getAllSliderAliases();
        if(count($slider_aliases) > 0) {
            foreach($slider_aliases as $slider_alias)
            {
                $field['choices'][$slider_alias] = $slider_alias;
            }
        } else {
            $field['choices'] = array( 'none' => 'No sliders exist. You must create one first.' );
        }
    } else {
        $field['choices'] = array( 'none' => 'Slider Revolution plugin was not found.' );
    }
    return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

Upvotes: 4

Harman
Harman

Reputation: 539

From Slider Revolution V6 the method "getAllSliderAliases" is not available anymore. New Code is :

functions.php

function my_acf_load_field($field)
{
    if (class_exists('RevSlider')) {
        $rev_slider = new RevSlider();
        $slider_aliases = $rev_slider->get_sliders();
        if (count($slider_aliases) > 0) {
            foreach ($slider_aliases as $slider_alias) {
                $field['choices'][$slider_alias->alias] = $slider_alias->alias;
            }
        } else {
            $field['choices'] = array('none' => 'No sliders exist. You must create one first.');
        }
    } else {
        $field['choices'] = array('none' => 'Slider Revolution plugin was not found.');
    }
    return $field;
}
//Only place these options on a specific select field with the alias "rev_slider"
add_filter('acf/load_field/name=rev_slider', 'my_acf_load_field');

page.php

$slider_alias = get_field('rev_slider');
if(!empty($slider_alias)) {
    echo do_shortcode(sprintf('[rev_slider alias="%s"]', $slider_alias));
}

Upvotes: 0

Andrew M
Andrew M

Reputation: 785

What you could do is the following. Revolution slider slider items are stored in a table called wp_revslider_sliders (the wp_ part may change based on how you set up the database - check the table name first)

You can query this table using the Wordpress $wpdb global and get back the alias field - which is used as the shortcode. So in the body of your load field function you could try something like this

function my_acf_load_field( $field ) {
    global $wpdb;
    $query = sprintf('select r.id, r.alias from %srevslider_sliders r',$wpdb->prefix);
    $sliders = $wpdb->get_results($query,OBJECT);

    foreach($sliders as $slider)
    {
        //This just formats the string with the correct short code
        $field['choices'][$slider->alias] = sprintf('[rev_slider alias="%s"]',$slider->alias);
    }

    return $field;
}

That should populate your dropdown with the right options - or at least get you on the right path

Upvotes: 1

Related Questions