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.
Related posts:
Now I see finally clearly. In the past I always had trouble with this plugin. Thanks for sharing.