How to dynamically add stylesheets to your wordpress theme

14/10-2009 | (0)

If you use a lot of different stylesheets for your theme, it can get a little tedious to add them all to your header.php file. You could import them in your style.css, but some of the great minification plugins do not support import.

If you organize all your stylesheets in a sub-folder in your theme, you can use the below function to automatically register and add them to your theme, provided that you use the wp_head() function in the head section of your header.php.

Usage:

  • Add the below code to your functions.php file in your theme folder.
  • Add your stylesheets to a sub-folder named css.
  • You can add a print stylesheet, by calling it print.css, for a good starting point, try out Hartija.
  • For handheld devices, you can add a stylesheet called either iphone.css or handheld.css.
  • Lastly you can add a stylesheet for projection, and yes it should be called projection.css.
  • All the above stylesheets should be placed in the CSS folder.
  • If you want to use a different location for your stylesheets, you can change the location in the last part of the themeCSSUrl and themeCSSDir variables, to point to your desired location.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<?php
add_action('wp_print_styles', 'add_stylesheets');
 
function add_stylesheets() {
  $themeCSSUrl =  get_bloginfo('template_url') . '/css/';
  $themeCSSDir = dirname(__FILE__). '/css/';
  if ($handle = opendir($themeCSSDir)) {
    while (false !== ($file = readdir($handle))) {
      if(end(explode(".",$file))=='css') {
        if($file=='print.css') {
          wp_register_style(current(explode(".",$file)), $themeCSSUrl.$file,array(),'0.8','print');
          wp_enqueue_style( current(explode(".",$file)));
        } elseif($file=='handheld.css' || $file=='iphone.css') {
          wp_register_style(current(explode(".",$file)), $themeCSSUrl.$file,array(),'1.0','handheld');
          wp_enqueue_style( current(explode(".",$file)));
        } elseif($file=='projection.css') {
          wp_register_style(current(explode(".",$file)), $themeCSSUrl.$file,array(),'1.0','projection');
          wp_enqueue_style( current(explode(".",$file)));
        } else {
          wp_register_style(current(explode(".",$file)), $themeCSSUrl.$file,array(),'1.0','screen');
          wp_enqueue_style( current(explode(".",$file)));
        }
      }
    }
  }
}
?>

WPreso Video Flow v0.3 released

01/10-2009 | (0)

This fixes an error discovered by Jonas from ZORN D SIGN in which Video Flow tried to include a document from Video FeatureBox, so that if you did not have WPreso Video FeatureBox installed as well, it would crash your whole site.

WPreso Video plugins updated to version 0.2

30/09-2009 | (2)

WPreso Video FeatureBox and WPreso Video Flow have been updated to version 0.2.

The dependency on the deprecated function mime_content_type() has been removed.

The function would check the mime type of the files in the plugin CSS directory and would then include the files which where CSS files.

Due to the fact that the function has been deprecated and some servers do not support the function, this validation has been removed. It now only checks the extension.

FileInfo might be used in the future, but as far as I have read, support for this could also be limited, so the best option is to wait until PHP 6 that should bring new built in support for file mime types.

WPreso Video Plugins released on WordPress.org Extend

16/09-2009 | (0)

Today I released two of my plugins

This is the first time I have release a plugin to the public, so there might be some quirks that I will have to figure out along the way, but I hope people will enjoy them.

I will also be releasing another video plugin soon, which is called WPreso Video Carousel, you can already view in action on the showcase site WPVideoTutorialSite.com. It is a Video Carousel, but I will have more info on it in a later post and on its own plugin page, until then, I hope you enjoy these two.

Launch of WPreso.com

15/09-2009 | (0)

WPreso launches with two WordPress plugins, WPreso Video FeatureBox and WPreso Video Flow, soon to be available on WordPress.org.

More info on the plugins can be found on their respective pages:

or go to the plugins page.

Hope you enjoy them.

This website uses a Hackadelic PlugIn, Hackadelic SEO Table Of Contents 1.7.3.