0

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

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 1

1

Can 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.

6
  • I think I've also tried this.. But to be sure of your suggestions... There would be no need for html_entity_decode($content); before the wrapping right?. And there's no need to call the kses.php from wp-include right? Jun 7, 2020 at 22:00
  • Update: After trying your solution, I get the same results as in the screenshot I shared with one difference. The "" (quotations) in all tags are stripped off Jun 7, 2020 at 22:27
  • I've added the code I used to test. And the code I mentioned above worked for me without any issue. Jun 7, 2020 at 23:52
  • No need to include anything. Just those code in functions.php that's what all I did to test. Jun 7, 2020 at 23:53
  • 1
    Apparently the plugin dealing with me was Jetpack I had to disable markups in Writing and all problems solved... I'll give you an up vote when ever I can... Thanks.. Jun 8, 2020 at 2:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.