Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question
    
Are the invalid attributes required or an accident? –  toscho Mar 23 '13 at 10:40
    
I think those are accidents :) –  Hasin Hayder Mar 23 '13 at 10:50
    
sorry for the typos, I corrected them... –  Remko Mar 23 '13 at 11:22
    
Have you tried Raw HTML and wrapping your <pre> blocks in the Raw HTML tag provided? –  infamouse Mar 25 '13 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

share|improve this answer
    
It will still work because the preg_replace_callback will match all instances of <pre>..</pre> –  Hasin Hayder Mar 23 '13 at 11:22
    
what's a good place (file) to put this in? –  Remko Mar 23 '13 at 11:25
1  
inside the functions.php inside the current theme you are using –  Hasin Hayder Mar 23 '13 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 '13 at 11:53
    
if you just run the convert_pre_entities (or html_entity_decode) once again, it will remove the double encodings. easy –  Hasin Hayder Mar 23 '13 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.

share|improve this answer
    
This plugin sounds like it could do the job, did you use a regex for it? –  Remko Mar 29 '13 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. –  Nathan Powell Mar 29 '13 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 '13 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 –  Nathan Powell Mar 29 '13 at 21:56
up vote 0 down vote accepted

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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