Most syntax highlighters use either shortcodes or make you add some obscure CSS classes to all <pre>
tags that you want to highlight. Even WordPress.com does that.
Introducing Code Prettify Syntax Highlighting Plugin
Code Prettify for WordPress is a new plugin with just 31 line of code that enqueues the Google Code Prettify library (one minified javascript file) which then parses all <pre>
tags on the page, detects the correct language and lazy-loads the necessary syntax module, and applies the highlighting.
No shortcodes, no custom CSS classes — just magic.
How’s it better than highlight.js?
Kaspar, it isn’t. Both of these (prettify.js and highligh.js) are my favourite syntax highlighting libraries. Do you prefer highlight.js?
Yep, I do. No clear benefits over google-code-prettify, though. Just stuck to one I found smallest and fastest.
Small and usefull.
I have also custom plugins on live sites with different js libs, very often highlight.js.
But a small hint. I think it is a good idea in your source to check for
if ( ! is_admin() )
before enqueue script. If it is only usable on frontend.Frank, notice that I’m using the
wp_enqueue_scripts
action to enqueue the script and that runs only on the front-end. Here is the description of that function:Thanks for your fast feedback. I mean that was not clear, I had read before long time the comment on core topic. But then is my comment off topic and maybe a hint for to debug this for clarify.
Yeah, Frank, that comment refers to “_script” — notice the missing “s” at the end, which I am referring to in my comment.
How make Indentation ?
Romain, simply wrap the code in
<pre></pre>
tags.Because i try with php and indentation not working. I have:
And not:
Romain, you can see it works if I wrap your sample code in
<pre></pre>
tags.But you need to write spaces to indent code ?
It doesn’t matter if you use tabs or spaces as long as it is wrapped in
<pre></pre>
tags. Browser will ignore any kind of indentation if it’s not inside<pre></pre>
.Ok, thank you ! Nice plugin ;)
Hi, I love this plugin since it has just 31 lines of code! :-)
Question, how do I change CSS type (“Skins”)? Let’s say I like “Desert” skin, do I load its CSS into my theme CSS?
Thanks!
There is a filter in the plugin that allows you to specify the “skin” you want to use. Something like this should work:
I’ve been using this for a while now, and am very happy with it. It’s much simpler to use than the Syntax Highlighter Evolved plugin I was using before, and it’s lighter on code too.
Is this available from the official WordPress plugin repo (https://wordpress.org/plugins)?
No, it isn’t, Nils. The plugin is too simple to provide support and answer questions on the official repository :)