How to use shortcode attribute in separate function

I’m working on a WordPress site and have installed a slider plugin that registers a slider on the backend (or multiple sliders) and then displays it on the front end with a couple of simple php snippets. I’m trying to modify the plugin so I can do the same thing with a shortcode. I want the shortcode to be able to register the plugin on the backend and display it on the front end using something like: [responsive_gallery slider_name="slider"].

I have that part down, and I can even display the slider using the shortcode. I’m just having trouble registering it on the backend. My code at the bottom works, but I was hoping there was a way to take the value stored in the shortcode attributes array ($slider_name) and use it in the set_flexslider_hg_rotators() function. I’ve been trying for hours and can’t get anything to work. Any suggestions on how to best do this?

Read More

Here is the plugin code:

Backend Registration:

function set_flexslider_hg_rotators()
{
  $rotators = array();
  $rotators['homepage'] = array( 'size' => 'homepage-rotator' );
  return $rotators;
}
add_filter('flexslider_hg_rotators', 'set_flexslider_hg_rotators');

Front End Display:

<?php if(function_exists('show_flexslider_rotator')) echo show_flexslider_rotator( 'homepage' ); ?>

Here is my working code:

function responsive_gallery_shortcode($atts, $content=null) {
    extract(shortcode_atts( array('slider_name' => 'product_page') , $atts));

    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');        

function set_flexslider_hg_rotators() {
    $rotators = array();
    $rotators[ 'product_page' ] = array( 'size' => 'product_page' );
    return $rotators;
}   
add_filter('flexslider_hg_rotators', 'set_flexslider_hg_rotators');

I want to be able to use $slider_name in the set_flexslider_hg_rotators function, but am unsure of what would be the best way to do that (or if it’s even possible). Would like it to look something like:

function set_flexslider_hg_rotators()
{
  $rotators = array();
  $rotators[$slider_name] = array( 'size' => $slider_name );
  return $rotators;
}
add_filter('flexslider_hg_rotators', 'set_flexslider_hg_rotators');

Since $slider_name is not global, nothing is returned from this function (as expected). Is there a way to so something similar?

Related posts

Leave a Reply

1 comment

  1. To be able to use a variable that is essentially being created within the shortcode function, you’ll need to store it somewhere and then retrieve the value. WordPress does use global variables internally to store and carry across values, but I wouldn’t advise you do the same though.

    Read about the options API here. Pretty simple functionality.

    So something like the following would work within your shortcode function:

    update_option('_unique_slider_name', $slider_name);
    

    Then inside of your filter function (or anywhere) you can access the value like this:

    $slider_name = get_option('_unique_slider_name');
    

    The value will be cached, so don’t worry about repeated database hits when you try accessing the value.