HACK: Fixing The Code Syntax Highlighter WordPress Plugins To Work With WYSIWYG
----
There are many code syntax highlighter plugins available for WordPress (e.g. iG Syntax Highlighter, WP-Syntax, etc) but almost all of them have a problem. They want you to write the code in “HTML editing mode”. If you use any kind of WYSIWYG or visual editors (like built in TinyMCE or offline clients’ similar modes), there is a grave problem. Your code becomes garbled. e.g.:
If you intend to write:
-
cat abc > /dev/null
It might turn out as:
-
</p>
-
<p> cat abc > /dev/null
-
</p><p>
The issue here is that the code highlighting plugins use the “exact” text entered within the [ code ] (or similar) tags, and since we used the WYSIWYG/visual editors, they add html formatting tags to the code and also convert some special characters into HTML entities (e.g. > into >, < into <, white space into , etc).
The workaround is that you can write the code in html editing mode. But the problem here is that if you happen to switch to the visual mode or edit your post in this mode any time later, the problem will come back which makes editing posts a pain. So, here is a simple hack that will let you write your code in visual/WYSIWYG mode without doing anything special.
Note: Although this hack is for iG:Synatx Highlighter plugin, but it can be added similarly to other plugins as well.
Step 1) Open the file syntax_hilite.php.
Step 2) Go to line no. 191 and delete the following 3 lines:
-
$arrSearch = array("< ", "< ", " >", " >", "< ", "< ", " >", " >");
-
$arrReplace = array("<", "<", ">", ">", "<", "<", ">", ">");
-
$inTxt = str_replace($arrSearch, $arrReplace, $inTxt);
Step 3) At the same place (from where you deleted the lines), add the following lines:
-
$inTxt = strip_tags($inTxt);
-
if (PHP_VERSION > 5.0)
-
{
-
$inTxt = html_entity_decode($inTxt, ENT_QUOTES, "UTF-8");
-
}
-
else
-
{
-
$arrSearch = array("<", ">", " ", "&");
-
$arrReplace = array("<", ">", " ", "&");
-
$inTxt = str_replace($arrSearch, $arrReplace, $inTxt);
-
}
Step 4) Save the file. That’s it. You’re done.
What we just did was strip out all the extraneous html formatting added by your editor and then convert the html entities into the characters that we want.
IMPORTANT: After this hack, you should write your code in visual mode. If you want to display some html code to your readers and write it in html mode of the editor then it might go boink. Otherwise it should be simple enough to create a new tag so that one set html editor code works and for another set, visual editor code works. But I would definitely recommend against it and anyways writing all code in visual mode, just like your other parts of your posts as its much more intuitive to write and easier to maintain and port to newer platforms.
For your reference, I’m attaching a copy of my own syntax_hilite.php here. File Attachment: syntax_hilite.php.shantz.zip (6 KB)
© Shantanu Goel | HACK: Fixing The Code Syntax Highlighter WordPress Plugins To Work With WYSIWYG----
If you liked this post, then you can get free updates










This post has 15 comments
March 28th, 2008
Shantanu,
I'm using wp-syntax, as it's the only one that really does what I want. Can you point me to the right place in the code as the problem you've described is really annoying indeed?
Thanks!
March 28th, 2008
@Artem: I'll try to look into it. But not much time right now, so might be possible by next weekend only..just remind me around next thursday..
March 29th, 2008
I am interested in a fix for WP-Syntax too! Thanks for your help!
March 30th, 2008
@Artem, Martin: I just took a cursory look at wp-syntax plugin. I think you can add the above mentioned lines in wp_syntax_code_trim function in wp-syntax.php, just before the "return $code" statement.
All you need to do is instead of $inTxt, you have to use $code in those lines.
I haven't tested it but I think it should work. Let me know if you face any issues.
March 30th, 2008
Thanks for the tip! But it doessn't have an effect.
It changes nothing at all, as far as I can see.
Switching from Wysiwig view to Code view always changes the < into a <.
March 30th, 2008
@Martin: It won't change anything while you are "editing" and switching between the wysiwig mode and code mode. It is supposed to "show" the change in the final output, i.e. on the page that your end-users will view. e.g. if you write some code in wysiwig mode and save it, then without the above mentioend change, ur users will see "& lt ;" (without the spaces) instead of <, but with this change they will see the character correctly.
March 30th, 2008
OK! Know I understand. I use only your code lines 8 to 10. I have php5 active but html_entity_decode doesn't seem to work correct.
Thanks it works perfect!!!!!!!
Have a nice rest-weekend.
August 31st, 2008
I do not believe this
August 31st, 2008
@fornetti: Do not believe what?
January 5th, 2009
I have written a plugin for Geshi highlighting using shortcodes. It works ok in Visual mode. I will appreciate all comment and suggestions.
http://wordpress.org/extend/plugins/wp-synhighlight/
January 5th, 2009
@FractalizeR
I've been waiting for someone to do that! I'll be testing your plugin shortly.
January 5th, 2009
Welcome

You can send all comments by email (address is on site and in README with plugin) or post on WP forum
January 5th, 2009
@Fractalizer: Cool. I'll try out the plugin. BTW, the problem I see with short code based solutions is that if/when such a plugin stops working, then that means one has to go through the old posts changing the code.
January 6th, 2009
You mean to remove shortcode itself? Yes, but if you don't like a plugin, you can easily make a stub just to get rid of shortcode.
January 6th, 2009
@Fractalizer: just found out that there is a simple function in wordpress to make your own shortcodes in a jiffy instead of preg_replacing them ourselves, maybe i can use the same in case of a plugin becoming obsolete to turn all such shortcodes into "pre" tags on the fly.
Anyways, I'll check out ur plugin soon. It's always good to see fellow developers' work
Trackbacks
Add a comment