Wordpress Plugin, get HTML post content

2020-07-18 02:36发布

问题:

I am building a wordpress plugin, that acts when the admin saves a new post. I need to get the content of that post and i am retrieving $_POST['content'] and $_POST['post_content'].
The problem here is that i only get the text inside that content, i need the html.

This way, if i was expecting something like <p>Lorem</p><p>ipsum</p><p>dolor</p>, I get Lorem ipsum dolor.

Can someone help me please?

Thanks

回答1:

Well, as @ChrisCarson points out, Wordpress doesn't store paragraph tags in the database by default.

So, what I need to do, is parse the paragraphs myself using:

$content = explode(PHP_EOL . PHP_EOL, $this->request['content']);
$htmlcontent = '';
foreach($content as $line){
    $htmlcontent .= '<p>' . str_replace(PHP_EOL, '<br />' , $line) . '</p>';
}   

thanks anyway



回答2:

Wordpress doesn't store paragraph tags in the database by default. Try this...

$content = trim(stripslashes($_POST["post_content"]));
$html = apply_filters("the_content", $content);


回答3:

From the codex: http://codex.wordpress.org/Template_Tags/the_content#Alternative_Usage

If you want to achieve the same output that the_content() returns, use the following code:

<?php
$content = apply_filters('the_content', $content);
$content = str_replace(']]>', ']]&gt;', $content);
?>