{"id":66,"date":"2007-05-14T09:31:06","date_gmt":"2007-05-14T13:31:06","guid":{"rendered":"http:\/\/scott.sherrillmix.com\/blog\/programmer\/wp_codeshield\/"},"modified":"2008-02-04T11:26:47","modified_gmt":"2008-02-04T15:26:47","slug":"wp_codeshield","status":"publish","type":"post","link":"http:\/\/scott.sherrillmix.com\/blog\/blogger\/wp_codeshield\/","title":{"rendered":"WP_CodeShield"},"content":{"rendered":"

I’ve been burned a few times in WordPress when posting something like:<\/p>\r\n

This is how you make text italic: This is italic.<\/em><\/code ><\/p><\/blockquote>\r\n

when I actually wanted:<\/p>\r\n

This is how you make text italic: <em>This is italic.<\/em><\/code><\/p><\/blockquote>\r\n

Of course it’s not too hard to remember to encode the special characters but this is WordPress and things are supposed to be easy. So I thought I would throw together a simple WordPress plugin to convert HTML special characters inside <code><\/code><\/code> tags. That was a few months ago and I got the plugin most of the way done and then got distracted with other things. But after reading about another person having the same annoyance<\/a>, I decided I should finish the plugin.<\/p>\r\n\r\n

So here is WP_CodeShield. It simply looks for <code><\/code><\/code> tags in posts and comments and makes sure the contents are encoded properly. You just type the code you want inside <code><\/code><\/code> tags and it should appear correctly. So you can type <code><em>This is italic.<\/em><\/code><\/code> instead of <code>&lt;em&gt;This is italic.&lt;\/em&gt;<\/code><\/code>.<\/p>\r\n

If you have any questions, suggestions or requests, please leave them in the comments below. You can also test WP_CodeShield out in the comments if you are curious.<\/p>\r\n\r\n

Current Version:<\/h3>

WP_CodeShield v0.4<\/a><\/p>\r\n\r\n

Installation instructions:<\/h3>

Unzip wp_codeshield.zip<\/code>. Upload wp_codeshield.php<\/code> to wp-content\/plugins\/<\/code> and active in the Plugin Control Panel. That’s it. Anything inside <code><\/code><\/code> tags will now automatically be converted to proper HTML.<\/p>\r\n\r\n

Technical Details<\/h3>\r\n

Most people should be OK just using WP_CodeShield naturally but for especially fancy users here are the more technical details: Nested code tags are ok (I’ve been using them in this post) but unequal numbers of <code><\/code> and <\/code><\/code> will cause the plugin to leave the entire post or comment alone. WP_CodeShield will leave any string which already contains HTML special characters alone. For example &lt;em&gt;<\/code> will be left alone. This is in case you have previously hand entered the special characters or want to describe something more complex than the plugin can handle. To keep track of these special occurrences, WP_CodeShield adds a small invisible comment tag (<!--formatted--><\/code>). WP_CodeShield only looks for <code><\/code><\/code> tags. <code ><\/code ><\/code> or <code class=\"fancy\"><\/code><\/code> will not be filtered. One small gotcha is that if a post contains <code class=\"fancy\"><\/code><\/code> no filtering will occur on that post (WP_CodeShield will think that the tags are unbalanced). You can get around this by making sure both the opening and closing tags do not match WP_CodeShield’s target like <code class=\"fancy\"><\/code ><\/code> (the extra space keeps WP_CodeShield from seeing the tags but are still valid HTML). <\/p> \r\n\r\n\r\n

Change Log:<\/h3>\r\n