How To: Allow Syntax Highlighting in WordPress Comments

I suppose this could be a plugin, but it seems too simple for all that… With SyntaxHighlighter Evolved installed, add the following PHP snippet to your functions.php file to allow syntax highlighting in your WordPress comments:
function highlight_comment_text() {
    if ( (array_key_exists('SyntaxHighlighter', $GLOBALS) ) {
        $highlighter = new SyntaxHighlighter();
        return $highlighter->parse_shortcodes(get_comment_text() );
    } else { return get_comment_text(); }
}
add_filter ('comment_text', 'highlight_comment_text');
The filter checks to see that the global variable $SyntaxHighlighter exists (and that SyntaxHighlighter Evolved is installed) then calls the SyntaxHighlighter::parse_shortcodes() function on your comment text. If SyntaxHighlighter Evolved is not installed, your comments are displayed normally. In my own (brief) testing, this works well for JavaScript and HTML, but not for PHP snippets. Edit: echo should be return to keep your comment formatting. Fixed 10/15/09
This entry was posted in wordpress and tagged , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

One Comment

  1. emko
    Posted November 2, 2009 at 10:14 am | Permalink

    i like your backgorund pic nice theme and good job bro

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>