Monday, 6 November 2017

Post Meta with Emojis Won’t Save


tldr: Convert your DB columns to utf8mb4 to support emojis / special characters everywhere in WordPress
tldr2; Or do a urlencode when saving, and urldecode when getting the post meta.
I recently did some client work that involved allowing users to generate their own image (canvas) that contained their name and a bunch of other decorative elements.
We allowed the users to change their name to customize the dynamically populated canvas. The name change was saved every edit via ajax as post meta so that their edits would become permanent.
Easy enough. I coded it, tested it and the new feature went live.
But when people started using it, literally within 5 minutes, I didn’t anticipate that people would decorate their names with emojis.
Some looked like this:
Benjamin
It was fun, but a bug had emerged. People who were adding emojis to their names weren’t saving in the ajax call.
update_post_meta Was Failing
After digging around, I found the code that wasn’t working as expected:
$text_with_emoji = sanitize_text_field( ‘happy ‘ );
update_post_meta( 1, ‘foo’, $text_with_emoji );

var_dump( get_post_meta( 1, ‘foo’, true ) );
// Will return null!
The text was being sanitized
Source: https://managewp.org/articles/16520/post-meta-with-emojis-won-t-save




source https://williechiu40.wordpress.com/2017/11/06/post-meta-with-emojis-wont-save/

No comments:

Post a Comment