8

I host my WordPress (v5.4.2) blog on 000webhost.com and I am unable to save any post with emoji characters. I get this error Updating failed. Could not update post in the database

I have tried changing these lines in wp-config.php

define( 'DB_CHARSET', 'utf8mb4' );

define( 'DB_COLLATE', 'utf8mb4_unicode_ci' );

I also migrated all my tables in MySQL to utf8mb4_unicode_ci with the below step. enter image description here

But none of these worked, what I am missing? Please help My WordPress version: 5.4.2

wp_encode_emoji()

1
  • I have the very same issue and tried all the first steps as having the right DB charset and collate. Moreover, it happened one day I haven't changed anything, which is strange and let me think it could be triggered by WordPress Crons.. Commented Feb 10, 2023 at 21:42

1 Answer 1

12

Adding this code in functions.php fixed it for me:

add_filter( 'wp_insert_post_data', function( $data, $postarr ) {
if ( ! empty( $data['post_content'] ) ) {
    $data['post_content'] = wp_encode_emoji( $data['post_content'] );
}
return $data;
}, 99, 2 );
3
  • 1
    This helped me when I added it to the end of the functions.php of the current theme using the Theme File Editor.
    – stormwild
    Commented Jun 24, 2023 at 6:31
  • 1
    I also had to include 'post_title' such as: $data['post_title'] = wp_encode_emoji( $data['post_title'] );
    – 23b
    Commented Mar 27 at 10:22
  • The main core of how his code works is wp_encode_emoji This WordPress function encodes any emojis in the post content into a format that is safe for saving in the database. It converts emojis to Unicode escape sequences (e.g., \u{1F600} for 😀) to ensure compatibility with databases that might not fully support native emoji storage.
    – mattmaldre
    Commented Dec 3 at 16:10

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