WordPress: updating failed. Could not insert post into the database.

W

The issue: in 2024, when I went back to update older posts on ArtsandBricks.com to make updates, many posts would error and not save. When I clicked “Update,” a red bar across the top of the page would appear that read, “Updating failed. Could not insert post into the database.”

The Arts and Bricks blog is hosted by WPMU DEV, which has excellent support; their team will help you troubleshoot anything WordPress-related (WOW)! When I shared my issue, the support team returned with, “We manually edited the post and re-saved it, and it worked; it means the issue is not the post or the _post table. The error is related to the content; I could save it after removing all. It seems there is something in the post that WP detects as invalid.”

The advice for troubleshooting was, “In such case, you might need to re-create the content on a new post, creating the blocks one by one, and saving until you can reproduce the issue; then you will get which block is conflicting with the saving.”

So, that’s what I started doing…Below are some tips to complete that troubleshooting step.

The best way to test and recreate posts

If you’re reading this post, you’ll likely be recreating older posts, too. Here are a few tips that might help you.

Keep the exact publication date:

If you’re recreating a new post, you can keep the original publish date by setting the publication date to the original date of the post. You can do this in the post editor by editing the publish date. (screenshot shown)

Keep the same URL:

If you’re recreating the post with the same content, the URL should remain the same as long as the post slug (the part of the URL that comes after the domain) remains unchanged. If you must ensure the URL remains the same, do not change the post slug. Also, for the original posts, edit the URL by appending extra characters; this will allow you to edit the URL and use the old URL to prevent duplicates.

The offending content: Emojis

I decided to test on a draft page before recreating pages. As I worked on copying and pasting from my live page to a test page, I found that content with emojis was causing the page to stop saving. The emojis were causing the “Updating failed. Could not insert post into the database” error.

Emoji Issue in WordPress Posts

By testing, I found that my issue was saving emojis in my WordPress posts. The fix for this is well documented in GitHub’s Gutenberg issue post, titled “Updating failed. Error message: Could not update post in the database when UTF-8 symbol in paragraph” I posted this solution to the GitHub post and several others have replied that it works for them too! Yay!

So here’s how you fix WordPress posts that are not saving because of an emoji issue.

First, you need to back up your site entirely. Then, you’ll update your wp-config.php file.

/** Database Charset to use in creating database tables. */
define( 'DB_CHARSET', 'utf8mb4' );

/** The Database Collate type. */
define('DB_COLLATE', 'utf8mb4_unicode_ci');

The above wp-config.php addition fixed some of my issues. For instance, it allowed me to create new posts with emojis and save them successfully. However, I struggled to get older posts that included emojis to update consistently.

The WPMU Dev team provided me with the wp_insert_post_data filter snippet of code to add to my functions.php file, which fixed the issue. The wp_encode_emoji function helps store emojis in your WordPress database using the UTF8 Character set. It can also be referenced on the developer.wordpress.org page.

I used the Snippets plugin to install this code rather than updating my functions.php file directly.

<?php
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 );

And another error: Not a valid JSON response

Around the same time as this emoji error, I ran into another error mentioning “not a valid JSON response.” This time, the error was because my post included the file name “wp-config.php” in the content. Here’s how I made it so my posts can include WP file names in the post’s content.

Happy WordPressing!

About the author

Kelly Barkhurst

Designer to Fullstack is my place to geek out and share tech solutions from my day-to-day as a graphic designer, programmer, and business owner (portfolio). I also write on Arts and Bricks, a parenting blog and decal shop that embraces my family’s love of Art and LEGO bricks!

Add comment

By Kelly Barkhurst

Recent Posts

Archives

Categories