the_content 过滤器不返回 html 代码,有什么替代品吗? [英] the_content filter doesn't return html code, any substitute?

查看:20
本文介绍了the_content 过滤器不返回 html 代码,有什么替代品吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个 WordPress 插件,我发现当使用 the_content 过滤器钩子时,它将帖子的内容作为参数提供给函数,但采用纯文本格式.我想获取帖子的 HTML 内容,有什么方法可以实现吗?

I was working on a WordPress plugin, and I found that when the_content filter hook is used, it gives content of the post to the function as a parameter, but in plain-text format. I want to get the HTML content of the post, any way to achieve this?

谢谢- 卡皮尔

推荐答案

终于找了好久,终于解决了.

Finally, after searching a lot, I had solved it.

正如 TheDeadMedic 所说 - 您确定帖子内容确实包含 HTML?不要忘记 WordPress 会即时添加段落,并且不一定将它们存储在数据库中."

As said by TheDeadMedic - "Are you sure that the post content does actually contain HTML? Don't forget WordPress will add paragraphs on-the-fly, and won't necessarily store them in the DB."

WordPress 通过使用一个名为 wpautop();

WordPress does by using a function called wpautop();

我只是将它与 get_the_content(); 一起使用,我让它工作了.

I just used this with get_the_content(); , and I got it working.

这是一个如何实现这一目标的示例 -

Here's an example of how you can achieve this -

function myPluginReplaceContent() {
    $content    =   wpautop(get_the_content());
    $content    .=  myPluginGetData(); // do whatever you want to - here
    return $content;
}

我发现此功能不会应用其他插件的过滤器.以下函数不会导致任何问题.

I found that this function won't apply filters by other plugins. The following function won't cause any issues.

function myPluginReplaceContent($thecontent) {      
    $thecontent .=  myPluginGetData(); // do whatever you want to - here
    return $content;
}

这篇关于the_content 过滤器不返回 html 代码,有什么替代品吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆