replace html entities in posts between pre tags

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
/pre

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.

Topic pre syntax-highlighting customization Wordpress

Category Web


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.


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

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.