I use the Google Syntax Highlighter for Wordpress plugin to display the code I share on this page all pretty-like.

The problem is, the thing isn’t very robust – it causes every single page on your blog to load up about 10 separate javascript files, regardless of whether the plugin is actually doing anything on that page or not.

I set out to optimize the code, but did a quick search first and found the folks at Fire Studios had already beaten me to it.

They have a couple of good tips on their page – they’ve combined the separate brush files into a single javascript file, and they’ve altered the code so that the files are only loaded on single posts and single pages.

That’s a great fix, but it wasn’t ideal for my site. On this site, most of my single posts and pages do not contain any code at all. So for like 95% of my pages, I don’t need to load the Google Syntax highlighter at all.

I expanded upon their customization with a further restriction – in my version, the plugin only loads if the page or post contains certain tags – namely “code”, “programming”, “perl”, “php”, “javascript”, or “python”, the tags I normally use when sharing a piece of code.

My google_syntax_highlighter.php file

function insert_header() {
   $current_path = get_option('siteurl') .'/wp-content/plugins/' . basename(dirname(__FILE__)) .'/';

    if(is_single() && has_tag(array('code','javascript','perl','python','php','programming'))){
   ?>

   <?php
   }
}

function insert_footer(){
   $current_path = get_option('siteurl') .'/wp-content/plugins/' . basename(dirname(__FILE__)) .'/';
    if(is_single() && has_tag(array('code','javascript','perl','python','php','programming'))){
   ?>



<?php
   }
}
add_action('wp_head','insert_header');
add_action('wp_footer','insert_footer');

Of course, you may want to use different tags, depending on your setup.