Home > web > wp_insert_post removes style elements

wp_insert_post removes style elements

Recently I was using the wp_insert_post function of wordpress to make a post through a plugin. The problem is that this function removed the styling element I included. For example I used this:

<div id="myid">Hello</div>

and after posting this through wp_insert_post it got like this:


I search for a solution for a couple of days but I couldn’t find a straight answer. Anyway, as it turned out it’s not such a hard problem. I am not a wordpress wizard (not even close :P) but I believe I solved my problem.

Locate the file /wp-includes/kses.php . This function is called when you make a post to filter out all the bad things that may be a risk for your blog. At line 44 (on my file) you can see an array of allowedPostTags. If you take a look you can see that what I was trying to add i.e. the id it is not conained in the div sub-array. That is the reason why it was removed. So I believe you can do 2 things:

  • You can use an allowed tag instead. This is what I did. I noticed that div class was an allowed tag so I used that instead of div id. Not much difference for me 🙂 and it worked.
  • I believe you can also manually add the id tag in the div sub-array in the same way as the other tags are added and it wont be removed. I have’t tested that but I am pretty sure it would work

Anyway, that’s all. I hope you can solve your problem. Let me know in the comments if you need some help.

See ya.

Related Posts:

Incoming search terms:

  • wp_insert_post (16)
Categories: web Tags:
  1. March 25th, 2011 at 11:12 | #1


    had exactly the same problem and found out that this happens whenever you are not logged in. Being logged in as a user it doesn’t make any problem.

    Somebody gave me the tip to use wp_set_current_user($dummyuserID); before posting.
    That turned out well.

    thx and have a nice day


  1. No trackbacks yet.