Escaping problem with WP-Syntax WordPress plugin
October 30, 2007
When I started this blog I had spent some time finding a syntax highlighter plug-in for the code snippets presented in the posts. I went to wordpress.org and I tried out some of the plug-ins, but I had some problems with all of them. After a few hours I found the WP-Syntax plug-in. I had had already some positive experience with the GeSHi (Generic Syntax Highlighter) library, and I liked the demo on the EmacsBlog so I thought this is what I wanted.
I tried it out and I noticed that it doesn’t look as fancy as in the demo page. After a fast search I found that I need the following CSS addition:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | .wp_syntax { color: #100; background-color: #f9f9f9; border: 1px solid silver; margin: 0 0 1.5em 0; overflow: auto; } /* IE FIX */ .wp_syntax { overflow-x: auto; overflow-y: hidden; padding-bottom: expression(this.scrollWidth > this.offsetWidth ? 15 : 0); width: 100%; } .wp_syntax table { border-collapse: collapse; } .wp_syntax div, .wp_syntax td { vertical-align: top; padding: 2px 4px; } .wp_syntax .line_numbers { text-align: right; background-color: #def; color: gray; overflow: visible; } /* potential overrides for other styles */ .wp_syntax pre { margin: 0; width: auto; float: none; clear: none; overflow: visible; } |
I added it to theme’s CSS, and I was happy. I started to finish my first post, but I noticed a very annoying behavior of the plug-in. The sample code contained “>” character, but in the post appeared “>”. I was a little disappointed, but I thought it will be easy to find the source of the problem.
First I looked into the source of the highlighter plug-in, and observed that GeSHi converts the HTML special chars to HTML entities with its own hcs() function. GeSHi could not be asked not to do this conversion, so I had to modify the plug-in a little:
79 80 | //$geshi = new GeSHi($code, $language); $geshi = new GeSHi(htmlspecialchars_decode($code), $language); |
Unfortunately the server where this blog runs has PHP older than 5.1, so I had to create my own version of htmlspecialchars_decode(), so I add this code to the plug-in too:
63 64 65 66 67 | if (!function_exists("htmlspecialchars_decode")) { function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) { return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style))); } } |
This little hack solved my problem, I hope it could be useful for others too.
Posted in
content rss

November 4th, 2007 at 3:14 am
[...] have already mentioned that I had difficulty in finding a suitable solution to post source code in my blog. However the [...]
March 9th, 2008 at 12:44 am
[...] a thanks goes out to Gergely Hodicska for the post, and fix, for escaping characters. Works like a [...]
April 17th, 2008 at 9:18 pm
[...] via: http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html [...]
June 2nd, 2008 at 4:39 pm
Hi, I’ve a problem with wp-syntax. It’s about CSS. When i try to make a code, it’s going out over the area. I used firefox problem. I see your syntax not like that. What i should edit with CSS file? see this Screenshot
August 3rd, 2008 at 3:21 am
[...] Gergely Hodicska has a solution on his [...]
September 21st, 2008 at 12:40 pm
Hi,
I had the same problem and solved it after reading this post.
Thank you for sharing !
I think there are other problems with the wp-syntax plugin but I did not have time to investigate yet. I saw that sometimes it converts newline chars to [br].
I will let you know if I solve other problems.
Thanks,
Mihai Bojin
October 5th, 2008 at 1:29 pm
[...] http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html [...]
October 30th, 2008 at 1:46 pm
thanks. just what i needed
November 19th, 2008 at 11:45 pm
Very interesting article, i bookmarked your blog
Best regards
November 22nd, 2008 at 10:28 am
hi , I just stopped by in search of wp_syntax. Do you have any solution to make code highlighter box smaller. The reason I am using custom developed have left and right hand menu, and it seems this goes down.
December 3rd, 2008 at 7:23 pm
Thanks for this. I found this page when searching for a solution to this wp-syntax problem.
December 27th, 2008 at 1:20 am
You are a god!
. Thanks for this little hack.
December 29th, 2008 at 10:38 pm
[...] few lines (1 if you server uses php 5.1) and the problems is solved. Here is a link to the answer. I test it and it’s working [...]
January 26th, 2009 at 10:46 pm
[...] problem previously. Â It turns out that I had modified the PHP source for the WP-Syntax plugin (as described here). Â I simply had to make the same change for the new version. Â I’m ignorant as to why [...]
January 27th, 2009 at 9:09 am
For the benefit of future visitors to this page, v0.9.2 of WP-Syntax now supports a new “escaped” attribute that you can use to declare that the code is already escaped. See Example 4 on this page: http://wordpress.org/extend/plugins/wp-syntax/other_notes/
August 15th, 2009 at 3:16 am
I can’t believe WP-Syntax hasn’t been updated to fix this annoyance. Ah well, thanks! Helped me out a lot
December 6th, 2009 at 12:14 am
Thanks for posting this – for the next person that stumbles upon it – setting escaped=”true” in the later versions accomplishes the same.
December 9th, 2009 at 11:53 am
[...] http://blog.felho.hu/escaping-problem-with-wp-syntax-wordpress-plugin.html Tag: escape, plugin, Wordpress Lascia un commento Commenti (0) Trackback (0) (iscriviti ai [...]
May 12th, 2010 at 1:45 pm
[...] WordPress Plugin WP-Syntax benutzt und Probleme mit den HTML special chars hat, sollte unbedingt hier mal reinschauen. KategorienComputer, Wordpress Tags: Software Kommentare (0) Trackbacks [...]
May 12th, 2010 at 1:47 pm
Thanks! Saved my life (:
May 20th, 2010 at 6:48 am
Thanks! This helped big time.
June 1st, 2010 at 10:19 pm