Code in posts

  1.  
  2.  
  3.  
  4.  
  5.  
  6.  
  7.  
  8.  
  9.  
<?php
function bbcode( $attr, $content = null ) {
        $content = clean_pre($content); // Clean pre-tags
        return '<pre"><code>' .
                str_replace('<', '<', $content) . // Escape < chars
                '</code></pre>';
    }
add_shortcode('code', 'bbcode');
?>

Instructions:

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 ]

Share this snippet

If you like this snippet, share it with friends!