Fire Studios

Archive

Contact

Categories

  • Coding
    • CSS
    • HTML/Javascript
    • PHP
  • Design
  • Freebies
  • General
  • Internet
  • Interviews
  • Plugins
  • Podcasts
  • Reviews
  • Screencasts
  • Tutorial
  • Weekday Tips
  • Wordpress

Podcast

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>

Reader's Thoughts

  • Noura Yehia

    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

    @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

    Nice tip. Thank you for sharing

  • Adam

    Very useful! Thank you ever so much.

  • avvocato spagna

    helpful and interesting

  • LeraJenkins

    Rather quite good topic

Your Thoughts?

 

This is that small text that tells you not to steal our stuff, but we don't really care too much.