Optimizing the “Google Syntax Highlighter” WP plugin

Optimizing the “Google Syntax Highlighter” WP plugin

Google Syntax Highlighter may be Wordpress' most popular syntax highlighter plugin, but it's not the most efficient. With plenty of extra JavaScript and sloppy PHP writing, it's quite the bloat-ware if left unchecked. So here's 2 fixes showing how to put GSH on a diet and still get all the features of the plugin.

Fix 1: Don't run on every page, just posts/articles

The way GSH is originally written it will load all its files on every page. That's not okay since, usually, we're only using this plugin in our posts and articles. To stop this, we're going to edit the "google_syntax_highlighter.php" file to this:

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

	if(is_single()){
	?>
		<link href="<?php echo $current_path; ?>Styles/SyntaxHighlighter.css" type="text/css" rel="stylesheet" />
	<?php
	}
}

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

	if(is_single()){
	?>
		<script type="text/javascript" src="<?php echo $current_path; ?>Scripts/shCore.js"></script>
		<script type="text/javascript" src="<?php echo $current_path; ?>Scripts/shBrushAll.js"></script>
		<script type="text/javascript">
			dp.SyntaxHighlighter.ClipboardSwf = '<?php echo $current_path; ?>Scripts/clipboard.swf';
			dp.SyntaxHighlighter.HighlightAll('code');
		</script>
		<?php
	}
}
add_action('wp_head','insert_header');
add_action('wp_footer','insert_footer');

Now we're using the Wordpress conditional tag "is_single()" to force the script only to run on single posts, not the home page, searches, or pages.

Fix 2: Combine all the languages into 1 file

You'll notice that in that example I've removed all the "shBush*.js" calls and replaced them with a single "shBushAll.js" call. This is simply a combined file of all the other javascript files provided.

Right-click "Save As" to download

<script type="text/javascript" src="<?php echo $current_path; ?>Scripts/shCore.js"></script>
<script type="text/javascript" src="<?php echo $current_path; ?>Scripts/shBrushAll.js"></script>
<script type="text/javascript">
	dp.SyntaxHighlighter.ClipboardSwf = '<?php echo $current_path; ?>Scripts/clipboard.swf';
	dp.SyntaxHighlighter.HighlightAll('code');
</script>

Share the Love Share the Love

Add to Reddit Add to StumbleUpon Add to Mixx Add to Delicious Add to designfloat

About the author: Fire G

Hey, I'm the founder of Fire Studios, and thus, have my hands in everything that goes on here at FS. I manage the content, moderate the comments, design everything, code everything, provides a lot of articles, host the official podcast FS-Air, and run/manage most of the other sites in the FI family. Often times I'll come to be working on so many things that I hardly accomplish much, but that's what makes me who I am.

6 Reader Comments

  •  
  • Noura Yehia
    May 22nd

    Google syntax highlighter consumes a lot, specially when you are not using all the supported languages that comes by default with the plugin.
    You said 2 fixes, where is the second one Fire G.?
    Thanks for this nice trick, it should come in handy :)

  • Fire G
    May 22nd

    @Noura Yehia: D'oh! Forgot to add in the title for the second fix, it was using the "shBrushAll.js" instead of all the regular scripts.

  • Raymond Selda
    May 22nd

    Nice tip. Thank you for sharing

  • Adam
    June 7th

    Very useful! Thank you ever so much.

  • avvocato spagna
    June 11th

    helpful and interesting

  • LeraJenkins
    June 22nd

    Rather quite good topic

Leave a Reply