I run a wordpress blog with a syntax highlighter plugin (Crayon). The plugin highlights code between pre tags, example:

<pre class="lang:languageid" title="example">
some code

Wordpress sometimes replaces characters suchs as " & < > with html entities which are shown in the code &quot; &amp; etc. I tried several plugins that should prevent this but they don't (always) work.

Is there an easy method to bulk edit old posts and search for entities between pre tags en replace them with the character? Or would it be easier to use a replace function on display and replace entities with their characters?

Any suggestions to prevent this in the future are welcome as well (perhaps extend/modify crayon?).

I usually write my blogs with Windows Live Writer but the behaviour also occurs when editing posts with the builtin editor.

  • Are the invalid attributes required or an accident?
    – fuxia
    Mar 23, 2013 at 10:40
  • I think those are accidents :) Mar 23, 2013 at 10:50
  • sorry for the typos, I corrected them...
    – Remko
    Mar 23, 2013 at 11:22
  • Have you tried Raw HTML and wrapping your <pre> blocks in the Raw HTML tag provided? Mar 25, 2013 at 23:33

3 Answers 3


This one should work

add_filter( 'the_content', 'pre_content_filter', 0 );

function pre_content_filter( $content ) {
    return preg_replace_callback( '|<pre.*>(.*)</pre|isU' , 'convert_pre_entities', $content );

function convert_pre_entities( $matches ) {
    return str_replace( $matches[1], html_entity_decode( $matches[1] ), $matches[0] );

Let me know

  • It will still work because the preg_replace_callback will match all instances of <pre>..</pre> Mar 23, 2013 at 11:22
  • what's a good place (file) to put this in?
    – Remko
    Mar 23, 2013 at 11:25
  • 1
    inside the functions.php inside the current theme you are using Mar 23, 2013 at 11:28
  • it seems to miss "double encoding" for example &amp;quot; can it be extended to catch that as well?
    – Remko
    Mar 23, 2013 at 11:53
  • if you just run the convert_pre_entities (or html_entity_decode) once again, it will remove the double encodings. easy Mar 23, 2013 at 14:00

WordPress replaces those characters when you are in the VISUAL editor. If you are writing a bunch of code, turn it off, and only write/save in the TEXT or HTML view.

EDIT: I just realized the "bulk edit" part of this question. I use a scary but powerful plugin called Search Regex for this type of thing.

  • This plugin sounds like it could do the job, did you use a regex for it?
    – Remko
    Mar 29, 2013 at 11:33
  • It will find/replace ANY string essentially. Type the string you are looking for eg:&amp; and click FIND. It will show you every occurrence in the database. You can at that point choose to replace with the & symbol. Mar 29, 2013 at 15:35
  • Seems better to limit this to entities within pre tags, the regex from @hasinhayder's answer nicely select the pre block(s) but how to find the entities in them and replace?
    – Remko
    Mar 29, 2013 at 16:39
  • The plugin will allow you to do a regular expression. I am not savy on that, but here is a good reference regular-expressions.info/reference.html Mar 29, 2013 at 21:56

Both answers are not the full solution, seems that if I combine them I am very close (just need to fix the replace part). The regex from @hasinhayder answer seems to work well in the plugin that @nathanpowell suggested.

There is no way to split points, common best practice seems to be to combine both answer in your answer and accept that one. I hope that's ok for both?

The answer is then:

Use the Search RegEx plugin for wordpress and use a regex expression |<pre.*>(.*)</pre|isU to match html entities within pre tags.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.