I'm having a problem with WPDB->insert inserting special characters.

I'm trying to insert a name (Jiří), the ř is being saved as ?.

The character is being able to inserted correctly into the database using add_option, which is strange.

Here is a version of the code which I'm trying:-

$memberdata = array( 'name' => 'Jiří', 'email' => 'someemail@address.com' );
add_option( 'temp_debug', $member_data['name'] ); // inserted as Jiří
$insert_into_temp=$wpdb->insert( wp_table, $member_data, array( '%s', '%s' ); // inserted as Ji?i

Any ideas?

  • The table where you insert the data with $wpdb->insert(), is it a custom table?
    – cybmeta
    Nov 17 '16 at 9:17
  • Can you show us SHOW CREATE TABLE your_wp_table?
    – kovshenin
    Nov 17 '16 at 10:12
  • CREATE TABLE wp_wp_email_capture_temp_members ( id int(255) NOT NULL AUTO_INCREMENT, name tinytext CHARACTER SET latin1 NOT NULL, email text CHARACTER SET latin1 NOT NULL, confirm_code text CHARACTER SET latin1 NOT NULL, date datetime NOT NULL, PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=23 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci ` @kovshenin
    – Rhys Wynne
    Nov 18 '16 at 12:59

If the same code works with add_option() and not with $wpdb->insert() in a custom table, it is probably because the collation of the custom table does not support the characters of the inserted data.

It shoulud work if you set the collation of your table to UTF-8, utf8_general_ci at minimum (to allow support for emojis, WordPress uses utf8mb4_unicode_ci since version 4.2).


Thanks to @kovshenin & @cybmeta above for putting me on the right lines :)

Although the tables were using utf8mb4_unicode_ci, the individual rows used latin1_swedish_ci.

Switching the rows to utf8mb4_unicode_ci, fixed this issue.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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