Skip to content

Instantly share code, notes, and snippets.

@dphiffer
Last active December 17, 2015 14:59
Show Gist options
  • Save dphiffer/5628511 to your computer and use it in GitHub Desktop.
Save dphiffer/5628511 to your computer and use it in GitHub Desktop.
Post meta escaping
$str1 = '\\"';
update_post_meta($post->ID, "test", $str1);
$str2 = get_post_meta($post->ID, "test", true);
echo "$str1<br>$str2";
/*
Result:
\"
"
More realistic use case...
Input: {"key":"value with \"quotes\" in the content."}
Output: {"key":"value with "quotes" in the content."}
The problem is that the output has returned something different than the input, making JSON data unparsable.
Solution from [wp-hackers] list: serialize the string on the way in, unserialize on the way out. <sigh>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment