Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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)

share|improve this question
    
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). –  TheDeadMedic Mar 13 '11 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 '11 at 2:45
    
That's a fair point @kristi, I merely thought best to highlight it for consideration. –  TheDeadMedic Mar 14 '11 at 4:37
add comment

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
    
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 '11 at 3:16
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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