1

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?

(Cross posted from stackoverflow I hope that's ok)

3
  • I'm sure this is doable, but it's worth mentioning beforehand that the plugin most likely behaves in this way for efficiency. Otherwise markdown would have to be rendered on-the-fly for all comments, all the time (as opposed to a less intensive one time render before saving). Mar 13, 2011 at 20:09
  • My blog is tiny, so I'm not worried about efficiency. The post content is saved in markdown, so this is probably not intended behavior. If the rendering was too inefficient, I would use a cache plugin, rather than save content rendered. And whether the plugin intended it or not, I want to save my content in markdown. The whole point of markdown is to have friendly human-editable source code.
    – kristi
    Mar 14, 2011 at 2:45
  • That's a fair point @kristi, I merely thought best to highlight it for consideration. Mar 14, 2011 at 4:37

2 Answers 2

3

This is a tricky one, but very doable. After looking at Mark's Markdown on Save plugin which does the exact thing you want but for posts content instead of comments i started thinking, that saving the comment content as Markdown would be bad because you would have to render the Markdown to HTML on-the-fly for each comment you display so the idea behind that plugin is that it saves the Markdown version as postmeta data and only displays it on the edit screen.

So that is exactly what you need to do and i can help you in getting started.

First you need to save the Markdown version of the comment content in the comment meta table using update_comment_meta and hooking it into wp_insert_comment which fires right after the comment is inserted in to the database:

//on comment creation
add_action('wp_insert_comment','save_markdown_wp_insert_comment',10,2);
function save_markdown_wp_insert_comment($comment_ID,$commmentdata) {
    if (isset($_GET['comment'])){
        $markdown = $_GET['comment'];
    }elseif (isset($_POST['comment'])){
        $markdown = $_POST['comment'];
    }
    if (isset($markdown)){
        update_comment_meta($comment_ID,'_markdown',$markdown);
    }
}

Then you need to show it on the comment edit screen using get_comment_meta and we hook it into comment_edit_pre filter which fires before the edit comment screen is displayed:

//on comment edit screen
add_filter('comment_edit_pre','load_markdown_on_commet_edit',10,1);
function load_markdown_on_commet_edit($content){
    $markdown = get_comment_meta($comment_ID,'_markdown',true);
    if (isset($markdown) && $markdown != '' && $markdown != false){
        return $markdown;
    }
    return $content;
}   

And Last we once again need to save the new Markdown version of the comment as comment meta data using once again update_comment_meta and we hook it into edit_comment which fires after a comment is updated/edited in the database:

//after comment edit screen
add_action('edit_comment','save_markdown_after_edit',10,2);
function save_markdown_after_edit($comment_ID){
    if (isset($_POST['content'])){
        update_comment_meta($comment_ID,'_markdown',$_POST['content']);
    }
}

Now I'm not sure how safe and secure this is or if this even works but it looks right to me and i'll leave this as a community wiki so everyone who knows better is welcome to correct me.

1
  • Thanks! I was hoping for something easy, since markdown-for-wordpress-and-bbpress is already doing everything else I want. When I get some time I'll try out this code, maybe add it into markdown on save.
    – kristi
    Mar 14, 2011 at 3:16
-1

Robin Adrianse's Parsedown for WordPress does what you want for both posts and comments. It's a bit tricky for complex posts as all posts are parsed as Markdown (HTML is still parsed as HTML is part of Markdown spec) but for comments it works fine. Either Markdown or HTML comments are supported, transparently. Nothing is permanently converted. Whatever you type in is converted to HTML on display.

I recommend switching to plain text editor to avoid any cross-conversion/parsing issues with the TinyMCE editor or more specifically WordPress's own reparsing of text. There are no performance issues I can see if one is using a caching plugin (who the heck isn't in 2021).

I specify Robin Adrianse's version of Parsedown as there are several other versions which go too far, converting HTML to Markdown permanently. Parsedown for WordPress works fine with WordPress 5.8 and Classic Editor so ignore any version warnings. Robin Adrianse no longer uses WordPress so cannot be bothered keeping up with the never-ending version number changing (real hassle for developers who are mostly volunteers providing open source code).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.