Support » Developing with WordPress » wp_insert_post fails when adding special characters

  • I have a function that uses wp_insert_post to add a post to the system and it works perfectly fine but if i try to add a title like Abadoğlu, Ender it adds up to the point where ğ is. In other terms, if there is any special character, the script stops at that point. But if i use the normal wordpress post page, I can add using the special characters

Viewing 10 replies - 1 through 10 (of 10 total)
  • You are probably not encoding the special characters before trying to insert them.

    actually i am reading them off of a text file so if they werent encoded i wouldnt be able to read them off the text.

    This is the query its running to add the post and if i run the query through phpmyadmin it works perfectly fine and when i print it out on to the page it prints out as is so I couldnt figure out where the problem is.

    INSERT INTO wp_posts(post_author,post_date,post_date_gmt,post_content_filtered,post_title,post_excerpt,post_status,post_type,comment_status,ping_status,post_password,post_name,to_ping,pinged,post_modified,post_modified_gmt,post_parent,menu_order,guid) VALUES ('1','2009-11-25 03:40:33','2009-11-25 01:40:33','','Abadoğlu, Ender','','publish','post','open','open','','abadodhlu-ender-21','','','2009-11-25 03:40:33','2009-11-25 01:40:33','0','0','')

    I am experiencing the exact same problem with other special characters. I’m importing a text file that was created with “special” characters(e.g. section sign, copyright symbol, ellipsis, euro). How can I get wp_insert_post to pass them through?

    make sure that your DB tables are utf8-collated. the mysql default is sweedish/latin-1, and that’ll fail with eastern-european characters (among others).

    Also, sometimes if you copy/paste text from a textfile the result depends on the encoding the textfile the text comes from has – so if you still encounter trouble try to change the encoding of your textfile(s) (and save them afterwards) BEFORE you copy text to some other document.



    use utf8_encode()



    I’m running into the same issue and utf8_encode() doesn’t help.



    For future reference, I’ve solved this problem:

    there is a bug in wp_check_invalid_utf8(), which is called by wp_insert_post().

    Ticket 11175

    instead of trying to fix bad encoded characters, it will simply just quit after it gets to the first invalid character. But the problem is more invovled than just that. Even if it continued over your input string, it still wouldn’t be what you want.

    The problem was that I was trying to load utf-8 from an older Drupal install db into wordpress. The MySQL table was utf encoded so I figured i could just simply call mysql_query(…) to pull from the old Drupal db and place that result into wp using wp_insert_post(). Infact, MYSQL doesn’t return utf-8 by default. you have to tell it to read the result from the db as utf-8 by calling this function:

    # Set character_set_results
    mysql_query(“SET character_set_results=utf8”, $connection); //we need this or else are results might be garbage

    (as a note, $connection is returned from mysql_connect(…) )


    Hi, I’m experiencing a crisis and am in need of dire help!

    I was trying to change my special character encode from UTF-8 to another one, because I’m trying to accommodate English, Spanish, and Japanese settings, but I accidentally typed “Unicode” for the encode and now my website looks like this:

    I would deeply appreciate it if someone could tell me how to restore my website so it’s back to displaying real html!!

    Thanks so much!!


Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘wp_insert_post fails when adding special characters’ is closed to new replies.