I’ve got a fever, and the only prescription is more cowbell.

Syntax Highlighter Optimization for Wordpress

Upon my initial activation of the “Google Syntax Highlighter for Wordpress” plugin, there was a noticable slowdown.  It is understandable to have a cutback in performance as a sacrafice for much needed functionality.  However, there is something that can be done to minimize that juggernaut of a plugin.

When I researched my syntax highlighter concerns, I ran into this article: Optimizing The “Google Syntax Highlighter” WP Plugin.  In this article, Fire G demonstrates 2 fixes for the google syntax highlighter wp plugin.  Fix 1 being the idea of only running the plugin on post pages.  Fix 2 is the concatination of all the language files into one.

In the header function, we now only link to the stylsheet if we are on a post page.

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
  }
}

In the footer function, I have combined all the syntax highlighter language files into shBrushAll.js.  I have also specified that I only want to use this code if we are on a post page.  I also have an added line for changing all pre tags with a title equal to code to pre tags with name equals code.  I do this for validation reasons, because name is not a valid attribute for a pre tag.

function insert_footer(){
  $current_path = get_option('siteurl') .'/wp-content/plugins/' . basename(dirname(__FILE__)) .'/';
  if(is_single()){
  ?>
    <script class="javascript" src="<?php echo $current_path; ?>Scripts/shCore.js"></script>
    <script class="javascript" src="<?php echo $current_path; ?>Scripts/shBrushAll.js"></script>
    <script class="javascript">
      jQuery("pre[title=code]").attr("name", "code");
      dp.SyntaxHighlighter.ClipboardSwf = '<?php echo $current_path; ?>Scripts/clipboard.swf';
      dp.SyntaxHighlighter.HighlightAll('code');
    </script>
    <?php
  }
}

In addition, I have added two lines to the SytanxHighlighter.css.  This is merely a preference and has no effect on performance.  I don’t like having my code wrap unexpectedly, especially for css definitions.

.dp-highlighter ol li, .dp-highlighter .columns div {white-space: nowrap;}
.dp-highlighter {overflow: hidden; overflow-x: auto;}

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

4 Comments to Syntax Highlighter Optimization for Wordpress

  1. June 4, 2009 at 6:19 pm | Permalink

    Hi, cool post. I have been wondering about this topic,so thanks for writing.

  2. September 22, 2009 at 8:31 am | Permalink

    great article thank you.

  3. Xaine's GravatarXaine
    January 22, 2010 at 6:39 pm | Permalink

    thanks great article!

    Shouldn't class="javascript" be changed to type="text/javascript" i notice it doesn't validate in your header either. Did my last comment get omitted?

Leave a Reply

You can 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>

Subscribe

Search

You Can Find Me On ...

Categories

RSS Other Articles By Me

  • reCAPTCHA: Blocking Spam and More
    A few months ago, I developed a ColdFusion/Mach-II implementation of the reCAPTCHA API.  This type of CAPTCHA requires the person to enter two words rather than a single sequence of random letters.  In addition to protecting your website from spam, reCAPTCHA helps digitize text books.  They do this by using a mystery word as one of the two words, until there […]
  • Using Friendfeed to Automatically Post Your Blog Entries on Twitter
    Friendfeed is a feed aggregator that groups together updates from twitter, facebook, digg, youtube, your blog and much more.  It can also publish entries from your friendfeed to twitter.  This article shows you how to tap into that twitter publishing feature of friendfeed to automate the posting of your blog entries to twitter.  Watch my screencast to create […]
  • Intense Debate Now on the Sitepro Blog!
    Recently on the Sitepro Blog, we have switched to using an enhanced commenting system called Intense Debate.  Intense Debate provides many features that will supercharge the community, increase comments, and increase pageviews.  Some notable features include commenter profiles, reputation scores, email notifications, reply by email, and facebook integration. […]
  • Sitepro Developers Attend TCCFUG
    Last Thursday, a few Sitepro developers attended the Twin Cities Coldfusion User Group at University of St. Thomas.  We were introduced to the future of Coldfusion, specifically Coldfusion 9.  There was free admission, free food, free shirt, prizes, and an abundance of new Coldfusion features. […]