WordPress plugins: Use conditional file loading

A post by Joost de Valk on Conditional Thickbox loading made me think on how this could be applied to a lot of plugins. For example, let’s take a look at Sociable.

Sociable is the plugin we all know, that shows those nice little chicklets to allow easy sharing of your articles via social networks and websites. Many people, including myself, aren’t showing these chicklets on their homepage or a static content page, but the stylesheet is loading! Why are we loading that stylesheet on those pages?

The function, including the hook, where Sociable loads its stylesheet is:

function sociable_css() {
 if (get_option('sociable_useiframe') == true) {
 global $sociablepluginpath;
 wp_enqueue_style('sociable-thickbox-css',$sociablepluginpath.'thickbox/thickbox.css');
 }
 if (get_option('sociable_usecss') == true) {
 global $sociablepluginpath;
 wp_enqueue_style('sociable-front-css',$sociablepluginpath.'sociable.css');
 }
}
add_action('wp_print_styles', 'sociable_css');

This function is called at the wp_print_styles hook and will therefor be included in each and every WordPress page, even when Sociable isn’t going to be displayed on that page. When we combine the comparing of the Sociable conditionals to the conditionals of the current page, like Sociable does in the function sociable_display_hook(), we can check if there is a need to include the stylesheet or not.

I think comparing these two conditionals should be implemented in a new function since Sociable needs to check it twice (for a iframe/thickbox and a custom stylesheet), so there is no need for coding this loop twice.

Off course, the Sociable stylesheet (852 bytes) isn’t that big. But this technique is applicable on much more plugins than just Sociable.

Share this post on social networks:

  • Twitter
  • Facebook
  • Digg
  • Sphinn
  • StumbleUpon
  • del.icio.us
  • Technorati

Related posts:

  1. Using conditional WordPress plugin file loading
  2. Get page ancestors the easy way in WordPress
  3. Create your very own WordPress plugin
This entry was posted in Archived. Bookmark the permalink.

2 Responses to WordPress plugins: Use conditional file loading

  1. Pingback: 20+ Tips Lengkap Optimasi WordPress

  2. Pingback: tips membuat loading wordpress enteng dan cepat | indonesia Go Blog ger

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>