the_content filter doesn't return html code, a

2019-08-29 07:39发布

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?

Thanks - Kapeel

标签: php wordpress
2条回答
Deceive 欺骗
2楼-- · 2019-08-29 08:19

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

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 does by using a function called wpautop();

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

EDIT :

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;
}
查看更多
【Aperson】
3楼-- · 2019-08-29 08:28

Do you have any plugins installed, or are you filtering the_content elsewhere?

By default, the content passed through the filter the_content is pretty much what's in the database, minus a bit of parsing (handling <!-- more --> teasers etc.).

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.

查看更多
登录 后发表回答