This question seem to be unanswered. I find myself stuck on it. Here it goes
I've a problem with WP API to insert a post with HTML Tags.
I'm using the method: wp_insert_post() and the content is like this:
$content = "<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>";
The result I want when I publish the post is:
A formatted post applying the html where necessary.
But the result I have when I publish the post is:
<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>
When I go to the WP Editor to edit the same POST Directly, The Visual Editor looks well formatted as expected
And The then I navigate to Text Editor as see this well formatted too:
<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>
It's quite obvious that this is what I actually want. But When I do this post from my php file as stated earlier, I get this published:
<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>
MY CODE
$content="<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>";
$my_post = array(
'post_title' => $title,
'post_status' => 'publish',
'post_content' => $content,
'post_author' => 1,
'post_category' => array(8,39));
$post_id = wp_insert_post( $my_post );
I HAVE TRIED html_entity_decode($content);
And
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
Before Inserting to DB and
add_filter('content_save_pre', 'wp_filter_post_kses');
add_filter('content_filtered_save_pre', 'wp_filter_post_kses');
After The insert
My script only have the wp-load.php
loaded.
Apparently This was a solution that finally worked.
$title = "My news";
$content='<p> <img src="img-link"> </p> <p> Hi, this is an example of the content.</p> <a class="dl" href="link-address"> Link Name</a>';
$postData = array(
'post_title' => $title,
'post_status' => 'publish',
'post_content' => $content,
'post_author' => 1,
'post_type' => 'post',
'post_category' => array()
);
//Here is the Magic:
kses_remove_filters(); //This Turns off kses
$id = wp_insert_post($postData);
kses_init_filters(); //This Turns on kses again
The Original Question was asked by me and a solution was provided on stackexchange HERE
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.