As you may have noticed, I often post code (including (X)HTML) in these posts. A classic problem is how to display a line like <html_tag> without having the web browser treat it as a tag and trying to process it. [UPDATE: Fix for comments!]
There’s an excellent plugin called Code Markup that handes this very nicely. Basically, it allows adding an attribute to the <code>
tag so that it reads (among other options)<code allow="none"
. Once this is done, all > and < characters are escaped, and no HTML will be processed.
The original plugin, however, does not work in comments. Thus, visitors cannot discuss code snippets! By making a two line change, however, this can be fixed.
Open up the file /path/to/wordpress/wp-content/plugins/code-markup.php
and look at line 41. There, the plugin registers two filters to be run on the_content
, which means a post. By changing the lines which read:
// Priority 1 - encode XML before we do anything else
add_filter('the_content', 'tguy_cmu_encode_xml', '1');
// Priority 11 - fix escaped slashes after wpautop() has added them
add_filter('the_content', 'tguy_cmu_fix_quotes', '11');
To have the following after them:
// Do the same for comments
add_filter('comment_text', 'tguy_cmu_encode_xml', '1');
add_filter('comment_text', 'tguy_cmu_fix_quotes', '11');
We can now have it apply this fix to comments as well! Go ahead, try it!
8 responses to “Posting Code in WordPress (and comments!)”
See? It works!
Regular:
Link?
With allow=”none”:
Link?
Thanks Mike! Great work. Your enhancement will be in the next version of Code Markup.
Ok. On line 109, there’s a check
if ($allow == 'none' || $allow == '')
. If you add the following below that, we can remove rel=”nofollow” from within the <code> tag. Normal links will still have rel=”nofollow”.The only problem with this is that if the user himself puts in rel=”nofollow”, it won’t be kept. I’m not sure (yet) how to fix that.
These regular expressions were taken from the DoFollow plugin.
Thanks. There’s one remaining issue that needs to be fixed — I don’t think wordpress should be allowed to insert rel=”nofollow” into the href’s wrapped by code. However, I can’t just remove all rel=”nofollow” lines, because the user may have put them there.
I’ll look into it, but I’m not sure exactly how to do this.
For this fix to work with the newest version of Code Markup, the last line of your patch needs to be changed to:
add_filter('comment_text', 'tguy_cmu_tidy_code', '11');
(
tguy_cmu_fix_quotes
seems to have been upgraded :))Thanks!,
Congratulations! That´s nice. Well done and good looking, it´s also easy to install, but I´m still looking for some more up-to-date stats map.
I will give this widget a little more time to satisfy me. Let´s see what happens.
great fix. why is this not integrated into Code Markup yet? hey btw do you guys know why it has started sticking rel=”nofollow” into markup? it only does it in a comment. even if markup=”none”. kinds of defeats the purpose.