How I added GeSHi to GPORG. Here I will demonstrate using GeSHi (for prettification purposes).

  1. <?php
  2. function sexy_code ($a, $content) {
  3. $attr = shortcode_atts(array(            // sets the defaults for each attribute so we don't run into errors if we're missing any
  4. 'lang' => 'html',                        // default language is HTML
  5. 'start' => '1'                           // which line num to start on
  6. ), $a);
  7. $content = strip_tags($content);         // remove wpautop() formatting from the snippet
  8. $content = preg_replace("#\&amp;([a-z0-9]+);#", "&amp;\\1;", $content);
  9. $content = str_replace('<', '<', $content); $content = str_replace('>', '>', $content);
  10. $lang = $attr['lang'];
  11. $geshi = new GeSHi($content, $lang);
  12. $geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS); // type of line numbers
  13. $geshi->enable_classes();                // turns on classes so we can prettify thru CSS
  14. if ($attr['start'] != 1)                 // if you specifically mentioned which line number you want the snippet to start on
  15. $geshi->start_line_numbers_at($attr['start']); // ...set it here
  16. return $geshi->parse_code();             // output it
  17. }
  18. add_shortcode( 'code', 'sexy_code' );    // shortcodify!
  19. ?>

(See the doc on WordPress’s shortcode API if you haven’t already.)

To use in a post…

[code lang="php" start="2"]echo $fun;[/code]

…will get you…

  1. echo $fun;