How can I get WordPress to save comments in markdown format?

I love markdown, and I have the WordPress markdown-for-wordpress-and-bbpress parsing markdown in my posts and comments.

However, I’ve noticed that WordPress saves the comments rendered in html format. This makes it more difficult to go back and edit comments. How can I get wordpress to save comments in markdown format?

Read More

I couldn’t find a plugin for it. Maybe there’s an easy php hack?

Edit:

Maybe it’s not builtin to wordpress. Comments normally not saved with any markup without the markdown plugin. Could be a markdown-for-wordpress-and-bbpress “feature” / accident?

Cross-posted to wordpress.stackexchange.com. BAinternet had some good ideas of saving markup for comments like in the markup-on-save plugin, but no working solution yet.

Partial hack

may help? May be theme-dependent. Lists still get saved rendered sometimes.

In wp-content/plugins/markdown-for-wordpress-and-bbpress/markdown.php comment out the pre_comment_content markdown filter

 if (MARKDOWN_WP_COMMENTS) {
    remove_filter('comment_text', 'wpautop', 30);
    remove_filter('comment_text', 'make_clickable');
    #HACK don't save comments rendered in HTML
    #add_filter('pre_comment_content', 'Markdown', 6);
    add_filter('pre_comment_content', 'mdwp_hide_tags', 8);
    add_filter('pre_comment_content', 'mdwp_show_tags', 12);
    add_filter('get_comment_text',    'Markdown', 6);
    add_filter('get_comment_excerpt', 'Markdown', 6);
    add_filter('get_comment_excerpt', 'mdwp_strip_p', 7);

Related posts

Leave a Reply

2 comments

  1. Nice question. As this feature is not available in the WordPress plugin, you’ll need to do some hackery at least to stop it from saving in HTML format, which you have done.

    Now you need for when the comments are displayed to process that markdown into HTML. So let’s use the comment_text hook:

    <?php add_filter('comment_text', 'Markdown'); ?>
    

    If you don’t want your original code to feel like “hackery” — turn it into a feature. Add a config option to the Markdown.php $save_format = 'html' or $save_format = 'markdown' then check if you want to execute the stripper function or not. In fact, you could be really smart and turn all of this into a function inside of Markdown.php (and remember to tell the author about your new feature, he might even update his original code 😉

    function set_save_format($format) {
    
      if ($format == 'markdown') {
        // Ok we need to change the format of any comments output to html:
        add_filter('comment_text', 'Markdown');
      }
    
    }