admin管理员组文章数量:1289635
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)
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 edited May 23, 2017 at 12:40 CommunityBot 1 asked Mar 13, 2011 at 19:41 kristikristi 2162 silver badges6 bronze badges 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). – TheDeadMedic Commented 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 Commented Mar 14, 2011 at 2:45
- That's a fair point @kristi, I merely thought best to highlight it for consideration. – TheDeadMedic Commented Mar 14, 2011 at 4:37
2 Answers
Reset to default 3This 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.
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).
本文标签: pluginsHow can I get Wordpress to save comments in markdown format
版权声明:本文标题:plugins - How can I get Wordpress to save comments in markdown format? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741461922a2380087.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论