1
votes

I'm importing posts to my wordpress site from different sources. The import is done via script, using wp_insert_post function which works just fine.

However, some posts come with a closing div that never starts and that messes up my theme.

I noticed that this </div> tag automatically disappears if I go to in wordpress dashboard -> edit post and simply switch from visual mode to html mode without having to manually remove the div.

Now the question: is there a function in wordpress codex that is doing that and that I could run for the post_content before running wp_insert_post?

Thank you!

1

1 Answers

1
votes

This is occurring because the WordPress editor automatically fixes incorrectly formatted HTML. It's possible to this server-side as well before inserting a new post.

The WordPress built-in function force_balance_tags will normalize unmatched HTML elements:

force_balance_tags("<p>unbalanced html</p></div>")

Will return:

<p>unbalanced html</p>