views:

64

answers:

3

Hi. Currently producing a Wordpress plugin that allows for multiple image sliders. At the moment, to make sure that the code is valid I am having to load each sliders dynamic styling into the tags. This is fine, however it loads the styling for all the sliders, which can really start to add a lot of code to the pages source if the users uses many image sliders.

So I'm trying to get the styling to only be added to the tags if the slider is actually displayed on the page. Is this possible? This is how I am currently displaying the styling:

function test() {
global $wpdb;
$table_name = $wpdb->prefix . "premiumslider";
$number = $wpdb->get_results("SELECT * FROM $table_name");
foreach ($number as $slider) { ?> 
<style type="text/css"><?php if($slider->paginationstyle == 'icons') { ?>
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li{<?php if($slider-        >paginationstyle=='icons'){ ?>background: url(<?php if($slider->paginationoff=='') echo WP_PLUGIN_URL.'/premium-slider/images/pagination.png'; if($slider->paginationoff!='') echo $slider->paginationoff; ?>) 0 0 no-repeat;<?php } ?>} 
#lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active{<?php if($slider->paginationstyle=='icons'){ ?>background: url(<?php if($slider->paginationon=='') echo WP_PLUGIN_URL.'/premium-slider/images/pagination_current.png'; if($slider->paginationon!='') echo $slider->paginationon; ?>) 0 0 no-repeat;<?php } ?>}
 <?php } ?>
 <?php if($slider->paginationstyle == 'images') { ?>
 #lofslidecontent<?php echo $slider->id; ?> .lof-navigator { margin-top: 50px; }
 #lofslidecontent<?php echo $slider->id; ?> .lof-navigator li img { border: <?php echo    $slider->imgborder; ?>px solid #<?php echo $slider->imgcolour; ?>; }
 #lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active img { border: <?php    echo $slider->imgborder; ?>px solid #<?php echo $slider->imghover; ?>; }
 #lofslidecontent<?php echo $slider->id; ?> .lof-navigator li.active img:hover { border: <?php echo $slider->imgborder; ?>px solid #<?php echo $slider->imghover; ?>; }
  <?php } ?></style><?php
 }
}

How can I achieve this?

To display the above code I use:

add_action('wp_head','test');

And to display the slider ($id being the id of that particular slider; 1, 2, 3, etc):

premium_slider($id);
A: 

This is sort of a complicated question. First of all, I disparage you for using a global variable, especially a DB resource. I'm also unclear as to why you need to echo the styles directly in this way.

Now to help you:

The only way to really solve this that I can see is if you have all of the styles stored somewhere, such as a DB, a config file, or any such thing per style. You must know the styles beforehand because you are hard coding them here. So just cycle through and if the ID of your slider matches its style, print the style. If you stored all of the styles in a big array in another php file hashed with the ID of the slider, this would be simple.

tandu
The global $wpdb you see is actually intended to be used as a global by Wordpress.
D Roddis
It can be passed as an argument to the function instead. Is Wordpress the king of design?
tandu
The global variable is needed to access the database, hence why none of this works if you don't add in; global $wpdb. How could you cycle through the ID's to find a match? I couldn't figure it out myself because you cannot pass an parametes to the function test(); because add_action('wp_head','test'); doesn't allow for them - or so I am lead to believe; maybe you can but I cannot figure out how too.
Matthew Ruddy
A: 

How do you determine which sliders appear on the page? Effectively you would need to alter your query to be the same as the one which actually loads the sliders, that way the CSS would only be loaded to display the same sliders.

$table_name = $wpdb->prefix . "premiumslider";
$number = $wpdb->get_results("SELECT * FROM $table_name WHERE category = 'cars'");

This would only load the elements into the foreach array that are displayed on that particular page.

D Roddis
What is the significance of category = 'cars' ?
Matthew Ruddy
A: 

@tandu: how can this be achieved? FYI, it won't let me comment on your answer.