1

I'm writing a WP plugin that connects to another WP sites, and get some data in return (some formidable entries, with names and other stuff).

Everything works great, and my plugin basicly works as its supposed to - but i noticed today that it have some strange encoding issue - i live in a nordic country, so ø æ l, is pretty common in names.

i have been debugging for the past 2 hours, but cant seem to figure out what is wrong.

**Data path:**
  End-point     -> Plugin - \00f8
  Data received           - \u00f8
  Data saved              - u00f8

i receieve the data with the following code:

$aDataRecevied = json_decode(oaal_CallEndpointAPI('GET',   'xxxxxx.dedi2491.your-server.de/wp-json/oaeu/v1/api', $aDataSend));
$sFrmEntriesRecevied = $aDataRecevied->sData->aFrmEntries;

and then i find my custom WP post, and save add/update it to the post with:

update_post_meta($iPost_ID, "aFrmEntries", json_encode($aFrmEntries), "");

The DB is runing: InnoDB & utf8mb4_unicode_ci, but that im very confident that wouldnt be a problem(?)

i'm not really sure what is wrong with this, but i hope somebody had an idea

the string the plugin gets from the endpoint looks like:

{"64":"H\u00f8jb","65":"Hansen","66":"asd@hotmail.com","date_created":"2016-11-21 13:11:26","form_id":"6"}

edit

Apparently the \ is removed by WP when i save the data to the DB

Mac
  • 43
  • 6
  • For us to help you, we're going to need the specific code that sends the `\u00f8`, or we're blind. – Williham Totland Nov 29 '16 at 09:29
  • I have attached(edit) my orginal post, with the returned string from the end point, it should be sufficient enough to prove the problem is not the end point itself, since it actually returns the correct string :) – Mac Nov 29 '16 at 09:33
  • Your slash is the wrong way. You want `\u00f8`. – Williham Totland Nov 29 '16 at 09:22

1 Answers1

0

So i figured it out! The problem is that WP escapes \u00f8, to make it more secure - sadly this has the side effect that \u00f8 will become u00f8.

I solved it by simply making sure that before i save the data, i convert it to ø æ å with the following:

$str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) {
    return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE');
}, $str);

Kudos to: https://stackoverflow.com/a/2934602/7166130

Community
  • 1
  • 1
Mac
  • 43
  • 6