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.
here is what I get with my real code... it seem it is inside <pre><code> $content </code></pre>
for some reason... I really need help with it:
UPDATE: I checked my database and the post includes this line <pre><code>
then the post itself </code></pre>
And I'm thinking if there's a way to get rid of those It might work pretty well.
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.
here is what I get with my real code... it seem it is inside <pre><code> $content </code></pre>
for some reason... I really need help with it:
UPDATE: I checked my database and the post includes this line <pre><code>
then the post itself </code></pre>
And I'm thinking if there's a way to get rid of those It might work pretty well.
1 Answer
Reset to default 1Can you try to wrap you wp_insert_post
call like bellow:
kses_remove_filters(); //This Turns off kses
$post_id = wp_insert_post( $my_post, true );
kses_init_filters(); //This Turns on kses again
So I tried your code with my method and it perfectly worked. Here is what I did.
$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()
);
kses_remove_filters();
$id = wp_insert_post($postData);
kses_init_filters();
It worked perfectly as it should. I'm using Twenty Twenty theme with no plugin enabled.