Skip to content

Instantly share code, notes, and snippets.

@BaylorRae
Created July 25, 2013 05:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BaylorRae/6077054 to your computer and use it in GitHub Desktop.
Save BaylorRae/6077054 to your computer and use it in GitHub Desktop.
<?php
// http://michelf.ca/projects/php-markdown/
require_once 'markdown.php';
/**
* parses markdown before printing on the page
*
* you could put this in MarkdownCommentParser::comment
* if you wanted to save off the html
*/
add_action('get_comment_text', function($comment) {
return MarkdownCommentParser::parse_comment($comment);
});
/**
* Acts as a placeholder for storing
* the original comment's text.
*/
class MarkdownCommentParser {
public static $comment;
public static function comment() {
return self::$comment;
}
/**
* Store the comment's original text
*/
public static function store_comment($commentdata) {
// try to use original comment data
// because of this: http://git.io/IRTh5w
if( isset($_POST['comment']) ) {
self::$comment = $_POST['comment'];
}else {
self::$comment = $commentdata['comment_content'];
}
// give the data back to wordpress
return $commentdata;
}
/**
* Default parsing configuration
*
* @link http://michelf.ca/projects/php-markdown/configuration/
*/
public static function parse_comment($comment) {
$parser = new \Michelf\Markdown;
$parser->no_markup = true;
return $parser->transform($comment);
}
}
// store off the comment as it came in
add_filter('preprocess_comment', array('MarkdownCommentParser', 'store_comment'), 5);
// "restore" the original comment
add_filter('pre_comment_content', array('MarkdownCommentParser', 'comment'), 15);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment