How to load google-code-prettify syntax highlighting without plugins

Photo of author
Written By Jan Reilink

Windows Server systems administrator & enthusiast.

If you run a WordPress blog where you display (parts of) source code, syntax highlighting is a must! It prettifies the code which makes it easier to read and it distinguishes code from text. However, most syntax highlighting is made available through plugins, and we all know too many plugins bring a lot of overhead to your blog.

Load google-code-prettify syntax highlighting in WordPress, without plugins

Conditional load in WordPress: Load javascript only on posts in WordPress.

Too many plugins and a code overhead results in a slow loading blog. We don’t want a slow blog, so here is how you can fix that, partially.

google-code-prettify is a nifty javascript module and CSS file that allows syntax highlighting of source code snippets in an HTML page. For WordPress it is available through the plugin Pretty GC Syntax Highlighter.

The disadvantage of such a WordPress plugin is: javascript and CSS files are always loaded, even on pages and in posts that have no source code to highlight. A waste of resources! Particularly in a world where websites have to load as fast as possible, and be as small as possible for mobile devices and (Google) SEO.

For WordPress, it is very easy to use the google-code-prettify javascript module instead of Prettify GC Syntax Highlighter. This makes a smaller footprint for your website.

To make this happen, download google-code-prettify and set it up somewhere on your website, or use the on hosted variant.

Open up your Theme functions.php or site-specific plugin file and add:

function custom_gcp_js() { if( is_single() ) { echo ' <script src="" async> </script> '; } } // Add hook for front-end <head></head> add_action( 'wp_head', 'custom_gcp_js' );
Code language: PHP (php)

This code loads the javascript only in the HTML head, only on posts.

Yes, you do loose some of the automatically converting of HTML tags to HTML-entities, but you can do that yourself easily.

Enqueue SyntaxHighlighter by Alex Gorbatchev in functions.php

If you wish to use Alex Gorbatchev’s SyntaxHighlighter, without a plugin like SyntaxHighlighter Evolved, then you can add the following code to your WordPress functions.php file:

function saotn_enqueueSyntaxHighlighter() { if ( is_singular() ) { wp_enqueue_style( 'sh-core', '//[theme name]/syntaxhighlighter/styles/shCore.css', array(), '3.0.83', 'all' ); wp_enqueue_style( 'sh-theme', '//[theme name]/syntaxhighlighter/styles/shThemeDefault.css', array(), '3.0.83', 'all' ); wp_enqueue_script( 'sh-jsCore', '//[theme name]/syntaxhighlighter/scripts/shCore.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsPerl', '//[theme name]/syntaxhighlighter/scripts/shBrushPerl.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsPHP', '//[theme name]/syntaxhighlighter/scripts/shBrushPhp.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsJScript', '//[theme name]/syntaxhighlighter/scripts/shBrushJScript.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsSql', '//[theme name]/syntaxhighlighter/scripts/shBrushSql.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsVB', '//[theme name]/syntaxhighlighter/scripts/shBrushVb.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsXML', '//[theme name]/syntaxhighlighter/scripts/shBrushXml.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsPowerShell', '//[theme name]/syntaxhighlighter/scripts/shBrushPowerShell.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsPlainText', '//[theme name]/syntaxhighlighter/scripts/shBrushPlain.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsCSS', '//[theme name]/syntaxhighlighter/scripts/shBrushCss.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsCPP', '//[theme name]/syntaxhighlighter/scripts/shBrushCpp.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsCSharp', '//[theme name]/syntaxhighlighter/scripts/shBrushCSharp.js', '', '3.0.83', true ); wp_enqueue_script( 'sh-jsBash', '//[theme name]/syntaxhighlighter/scripts/shBrushBash.js', '', '3.0.83', true ); } } add_action( 'wp_enqueue_scripts', 'saotn_enqueueSyntaxHighlighter', 20 );
Code language: PHP (php)

As per installation instructions, the shCore.js, shCore.css, shCore.css and shThemeDefault.css are required. You have to create the folder /syntaxhighlighter/ (with scripts/ and styles as subfolders/) in your theme directory.

Of course you can create your own plugin for this too.

SyntaxHighlighter shortcode

I can imagine you don’t want to type out the full <pre class="brush: php"> ... </pre> constantly. Create your own shortcode for it! In your functions.php (or site-specific plugin file), simply add:

function saotn_php( $atts, $content = null ) { return '<pre class="brush: php">' . $content . '</pre>'; } add_shortcode( 'php', 'saotn_php' );
Code language: PHP (php)

This provides the [php] your content [/php] shortcode.

For support in your comments form, add

add_filter( 'comment_text', 'do_shortcode' );
Code language: PHP (php)

WordPress is_single() vs. is_singular()

What’s the difference between “is_single()” and “is_singular()?

There is a difference in the WordPress functions is_single() and is_singular(), mainly: is_single() returns TRUE for any single post being displayed, and is_singular() returns TRUE for any page, attachment, or single post that is displayed. Along with is_page(), these are WordPress’s conditional tags.

Did you like: How to load google-code-prettify syntax highlighting without plugins

Then please, take a second to support Sysadmins of the North and donate!

Your generosity helps pay for the ongoing costs associated with running this website like coffee, hosting services, library mirrors, domain renewals, time for article research, and coffee, just to name a few.

Hi! Join the discussion, leave a reply!