Say I created a plugin that injects content (used as a shortcode) into a post. I’m trying to push that code for each post to the bottom of the page.
This works fine, however when you are on an archive page with multiple posts using the same shortcode, there is an obvious conflict because the function names become duplicate and it will only output the footer code (activate_flex_slider) once.
<?php
function your_function() {
echo '<p>This is inserted at the bottom</p>';
}
add_action('wp_footer', 'your_function');
?>
What i’m trying to do is output footer script so that there are multiple jQuery instances pointing to their respective ID’s…
<?php
function flex_slider() {
$output ='<ul class="flexslider"><li>Slide Content</li></ul>';
return $output;
}
function activate_flex_slider(){
?>
<script>
( function ($) {
$(window).load(function(){
//different number will be prepended to ID (matches post ID #)
$('#carousel-<?php echo $post->ID ?>').flexslider();
});
})(jQuery);
</script>
<?php
}
// Hook into footer so slider becomes active after page loads
add_action('wp_footer','activate_flex_slider');
// Create the Shortcode
add_shortcode('flex_slider', 'flex_slider');
?>
I would wrap the whole thing in a class and put your data into a class var.
I’ve used Flexslider before and still had a plugin for shortcodes lying around. It’s a bit dusty, but I’ve uploaded it to my dev install now that I came across this question and it still works (and does so with multiple sliders on the same page), so here’s the main plugin file’s contents:
Flexslider with WP Shortcodes
In this plugin I initialize all the sliders with the same properties, so for the initialization.js above
will suffice.
Note that you only need multiple jQuery functions if you want to use the slider with different properties each time. Otherwise you can target all of them by using a class as a selector (as is done above).
Varying Properties
If you must have varying properties, this is how that’d be done (I will assume you have an array of properties from somewhere – postmeta, options, whatever). The array would look like this:
And then you could add
to the main plugin class and as for the js do
And with that you’d have multiple instances of the slider, each with it’s own set of arguments.