Search code examples
phparrayswordpressshortcode

Save shortcode array with WordPress Options API then output in another function


Note: While this is probably a simple fix, I'm new to using arrays and am completely stumped.

I'm trying to save the data from a shortcode array via the Options API in WordPress, then call that array and use the data to create another array to hook into a plugin's function. It's a responsive slider plugin and I'm basically trying to attach a shortcode to it so I can create the slider on the backend and display it on the front end with a shortcode that looks like: [responsive_slider slider_name="imageslider"].

The implementation documentation can be found here, and here's my code:

function responsive_gallery_shortcode($atts, $content=null) {
    extract(shortcode_atts( array('slider_name' => 'product_page') , $atts));
    foreach ($slider_name as $value) {
        update_option('_unique_slider_name', $value );
    }
    if(function_exists('show_flexslider_rotator')) 
        echo show_flexslider_rotator( $slider_name );

    add_image_size( $slider_name , '550', '250', true );
}
add_shortcode('responsive_gallery', 'responsive_gallery_shortcode');        

if (!function_exists('custom_set_flexslider_hg_rotators')) {
    function custom_set_flexslider_hg_rotators() {
        $slider_name = get_option('_unique_slider_name');

        foreach ($slider_name as $value) { 
            $rotators = array();
            $rotators[ $value ] = array( 'size' => $value );    
            return $rotators;
        }   
    }  
} 
add_filter('flexslider_hg_rotators', 'custom_set_flexslider_hg_rotators', 9999);

I'm getting an "Invalid argument supplied for foreach()" error on both foreach functions. On the page where I have two shortcodes both errors show up twice. It seems as though $slider_name is a string instead of an array, but there's got to be a way to save it in the update_option() function so that it returns an array. I'm quite new to arrays, and I'm definitely struggling here. I've spent hours on this and have already received a little help on the WordPress side, but I'm not quite getting it.


Solution

  • As the shortcode attribute will arrive as a string, you need to convert it to an array first.

    At the same time, as it has to be passed as a string, you'll need to use a separator so you can manage this.

    And for all that, you'll need the PHP function explode.

    $string = "one,two";
    $array = explode( ',', $string );
    var_dump( $array );
    

    Results in:

    array (size=2)
      0 => string 'one' (length=3)
      1 => string 'two' (length=3)
    

    And

    $string = "one";
    $array = explode( ',', $string );
    var_dump( $array );
    

    Results in:

    array (size=1)
      0 => string 'one' (length=3)
    

    PS: It's always worth to consult the PHP Manual and also the comments in each of its pages : http://www.php.net/manual/en/language.types.array.php


    [update]
    There are many issues with your original code, check the comments of this revised version:

    function responsive_gallery_shortcode($atts, $content=null) {
        extract(shortcode_atts( array('slider_name' => 'product_page') , $atts));
    
        // Convert string into array
        // Using comma as separator when writing the shortcode in the post
        $array_slider = explode( ',', $slider_name );
    
        // YOU DON'T NEED A `foreach` HERE
        //foreach ($array_slider as $value) {
            update_option('_unique_slider_name', $array_slider );
        //}
    
        // I DON'T KNOW WHAT THIS FUNCTIONS DOES
        // But in any case, being $array_slider an array, maybe it should be inside a `foreach`
        if(function_exists('show_flexslider_rotator')) 
            echo show_flexslider_rotator( $array_slider );
    
        // THIS DOESN'T MAKE SENSE
        // You're not supposed to be adding images sizes at each Shortcode call
        // And you are dealing with an array
        add_image_size( $slider_name , '550', '250', true );
    }
    add_shortcode('responsive_gallery', 'responsive_gallery_shortcode');        
    
    if (!function_exists('custom_set_flexslider_hg_rotators')) {
        function custom_set_flexslider_hg_rotators() {
            // The option was already saved as array, so we can work directly with it
            $slider_name = get_option('_unique_slider_name');
    
            // YOU DON'T WANT TO DECLARE THE VARIABLE AT EACH STEP OF THE LOOP
            $rotators = array();
            foreach ($slider_name as $value) { 
                $rotators[ $value ] = array( 'size' => $value );    
            }   
            // RETURN THE VALUE ONLY AFTER COMPLETING THE LOOP
            return $rotators;
        }  
        // PUT THE FILTER HOOK INSIDE if(!function_exists())
        add_filter('flexslider_hg_rotators', 'custom_set_flexslider_hg_rotators', 9999);
    }