如何让 WordPress 以 Markdown 格式保存评论?

发布于 2024-10-21 19:12:43 字数 1385 浏览 3 评论 0原文

我喜欢 Markdown,并且我有 Wordpress markdown-for-wordpress-and -bbpress 解析我的帖子和评论中的降价。

但是,我注意到 WordPress 保存以 html 格式呈现的评论。这使得返回并编辑评论变得更加困难。如何让 WordPress 以 Markdown 格式保存评论?

我找不到它的插件。也许有一个简单的 php hack ?

编辑:

也许它不是内置于 wordpress 的。如果没有 Markdown 插件,评论通常不会以任何标记保存。可能是 wordpress 和 bbpress 的降价“功能”/意外吗?

交叉发布到 wordpress.stackexchange。 com。 BAinternet 有一些保存评论标记的好主意,例如在保存标记插件中,但还没有可行的解决方案。

部分破解

可能有帮助吗?可能取决于主题。有时列表仍然会保存渲染。

wp-content/plugins/markdown-for-wordpress-and-bbpress/markdown.php 中注释掉 pre_comment_content markdown筛选

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水晶透心 2024-10-28 19:12:43

好问题。由于此功能在 Wordpress 插件中不可用,您需要进行一些修改,至少阻止它以 HTML 格式保存(您已经这样做了)。

现在,您需要在显示注释时将 markdown 处理为 HTML。因此,让我们使用 comment_text 挂钩:

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

如果您不希望原始代码感觉像“ hackery”——将其变成一项功能。在 Markdown.php $save_format = 'html'$save_format = 'markdown' 中添加一个配置选项,然后检查是否要执行 stripper 函数。事实上,你可以非常聪明,将所有这些变成 Markdown.php 中的一个函数(并且记得告诉作者你的新功能,他甚至可能更新他的原始代码;)

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

}

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

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文