Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

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);
like image 424
k107 Avatar asked Mar 12 '11 23:03

k107


People also ask

How do I turn on Markdown in WordPress?

First thing you need to do is install and activate the WP-Markdown plugin. Upon activation, you need to visit Settings » Writing and scroll down to Markdown section. You can enable WP-Markdown for posts, pages, and custom post types. You can also enable Markdown support for comments.


2 Answers

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');
  }

}
like image 194
Gary Green Avatar answered Sep 27 '22 16:09

Gary Green


I guess u can use the http://adambrown.info/p/wp_hooks/hook/comment_save_pre-hook to manipulate the data.

like image 37
Jarsäter Avatar answered Sep 27 '22 16:09

Jarsäter