wp_insert_post() with HTML tags using PHP

Question

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:

enter image description here

0
, , DigitalBraine 4 years 2020-06-07T09:10:22-05:00 0 Answers 97 views 0

Leave an answer

Browse
Browse