encoding - wp_insert_post and title not utf8 inserts with empty title?

admin2025-01-08  5

I'm using wp_insert_post I loop over a text file one row at a time and for each row, I create a post. The text is set as the `post_title', for the text that is not utf8 the post inserts but with an empty title.

Why does that happen, if I'm able to create a post in the backend admin using non-utf8 chars it looks like WordPress converts the encoding in the backend.

How can I bypass this with wp_insert_post and have it insert the post title with non-utf8 chars?

Thanks

I'm using wp_insert_post I loop over a text file one row at a time and for each row, I create a post. The text is set as the `post_title', for the text that is not utf8 the post inserts but with an empty title.

Why does that happen, if I'm able to create a post in the backend admin using non-utf8 chars it looks like WordPress converts the encoding in the backend.

How can I bypass this with wp_insert_post and have it insert the post title with non-utf8 chars?

Thanks

Share Improve this question edited Sep 26, 2022 at 14:06 seramo 133 bronze badges asked Sep 20, 2013 at 16:47 AnagioAnagio 9533 gold badges20 silver badges47 bronze badges 3
  • 1 Why do you want to insert invalid data into your database? This will break everything. And no, you cannot enter non-UTF-8 characters in the back end. – fuxia Commented Sep 20, 2013 at 16:51
  • @Toscho I just tested entering ā into a custom field from the backend and it saved. WordPress is correcting the slug which is great, and keeps the title and permalink in the backend but why not when I use wp_insert_post isn't it running the same actions as from the backend to create the posts? – Anagio Commented Sep 20, 2013 at 16:56
  • 3 ā is a valid UTF-8 character, depending on the encoding. You have to make sure the correct encoding is used, or MySQL must reject the input. See this function for one way to do that. – fuxia Commented Sep 20, 2013 at 17:05
Add a comment  | 

2 Answers 2

Reset to default 0

It could be due to the text encoding you are using is not set to UTF8, try to use iconv() and set.

iconv('ISO-8859-1','UTF-8', $post_info['post_content']);

This is where the WordPress data sanitization functions come in handy, specifically sanitize_text_field(). Try using that function on your text before/while inserting it. This should also be safe for the lines that are currently working.

wp_insert_post(array(
   'post_title' => sanitize_text_field( $text_from_row )
));
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266361a1114.html

最新回复(0)