Code in posts

If you run a blog that focuses on programming you probably want to be able to display code in your posts. This is the shortcode to escape the and clean up WordPress auto formatting inside the pre-tags.

Usage(remove the space from the closing short code):

[ code ]<?php echo 'Hello World!' ?>[ /code ]

Code Snippet:

function bbcode( $attr, $content = null ) {
        $content = clean_pre($content); // Clean pre-tags
        return '<pre"><code>' .
                str_replace('<', '<', $content) . // Escape < chars
add_shortcode('code', 'bbcode');

Posted in Admin area, Posts