Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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?

share|improve this question
add comment

1 Answer 1

up vote 1 down vote accepted

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.

share|improve this answer
    
Thanks for the help! It wasn't what I was looking for, but thank you. The functions I put in my question work, just as the functions work in your answer. The problem is that I'm trying to take the data stored in the shortcode attributes array and use it in the custom_set_flexslider_hg_rotators function. In the responsive_gallery_shortcode function. I'd like to be able to use $slider_name (which is defined by the shortcode array) so I can do something like: $rotators[ $slider_name ] = array( 'size' => $slider_name ); I just don't know how to do this, or if there's a way to do this. –  Jeremiah Prummer Feb 26 '13 at 15:57
    
Also, as far as the missing function show_flexslider_rotator goes, I didn't include it because I'm trying to hook into the the function according to the function's guidelines. There don't seem to be any issues with the way the function works, I'm just trying to set all of it through a shortcode, which is not a feature of the plugin. –  Jeremiah Prummer Feb 26 '13 at 15:59
    
Oh, I get you. Well the variable is defined locally within the scope of the shortcode function so to access it you're going to have to store the value somewhere to be accessed from within your filter function. Global variables would work (and Wordpress does extensively use them internally), although if I were you I'd use something like the transients API. I'll update my question for you. –  DigitalSea Feb 26 '13 at 22:57
    
Thanks for the tip about the transients API. I've never heard of it before, but am looking into it. Reading an article from an answer by you that I found. –  Jeremiah Prummer Feb 27 '13 at 0:14
    
No worries man. The Transients API is good for situations where you don't need to always have the value saved, you can specify an expiration. However, in my answer I mention the options API as it will save the value to the database and only remove itself when you delete it. –  DigitalSea Feb 27 '13 at 1:08
show 3 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.