Reputation: 349
In my wordpress theme there is a custom post type named songs and in that post type there is a custom field named playlist which contain songs information like artist name,download link,song lyric etc...
My problem is that this custom field is serialized in json version of my website and i don't know how to fix that.
I use json api plugin for word press.
here is an example of what it shows in json
...
"custom_fields": {
...
"playlist": [
"a:1:{i:0;a:19:{s:5:\"title\";s:44:\"Hosein Tohi And Sami Beigi - Ba Man Miraghsi\";s:3:\"mp3\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20128.mp3\";s:7:\"radioip\";s:0:\"\";s:9:\"radioport\";s:0:\"\";s:11:\"buy_title_a\";s:14:\"\u06a9\u06cc\u0641\u06cc\u062a 320\";s:10:\"buy_icon_a\";s:14:\"cloud-download\";s:10:\"buy_link_a\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20320.mp3\";s:11:\"buy_title_b\";s:14:\"\u06a9\u06cc\u0641\u06cc\u062a 128\";s:10:\"buy_icon_b\";s:14:\"cloud-download\";s:10:\"buy_link_b\";s:134:\"http:\/\/dl.paradi3emusic.com\/Musics\/Aban%2094\/Persian\/Single\/Hosein%20Tohi%20And%20Sami%20Beigi%20-%20Ba%20Man%20Miraghsi%20-%20128.mp3\";s:11:\"buy_title_c\";s:0:\"\";s:10:\"buy_icon_c\";s:14:\"cloud-download\";s:10:\"buy_link_c\";s:0:\"\";s:11:\"buy_title_d\";s:0:\"\";s:10:\"buy_icon_d\";s:14:\"cloud-download\";s:10:\"buy_link_d\";s:0:\"\";s:10:\"buy_custom\";s:0:\"\";s:11:\"lyric_title\";s:0:\"\";s:5:\"lyric\";s:0:\"\";}}"
],
...
},
...
****EDIT*****
I want to have another custom field which contain json array of these serialized data but unserialized also I tried this but didn't worked.
add_post_meta($id, 'myplaylist1', $playlist);
Upvotes: 0
Views: 1001
Reputation: 5311
You can use wordpress built in maybe_unserialize() function to unserialize a serialize data
https://codex.wordpress.org/Function_Reference/maybe_unserialize
Upvotes: 1
Reputation: 20469
You will need to write a filter to deserialize the content before it is output by the json api plugin.
There is a filter applied by the plugin just before the json is output, documented here:
https://en-gb.wordpress.org/plugins/json-api/other_notes/#5.-Extending-JSON-API
That would be suitable for this task.
Add the following to your themes functions.php
:
add_filter('json_api_encode', function($response){
if (isset($response['posts'])) {
foreach ($response['posts'] as $post) {
deserialize_playlist($post);
}
} else if (isset($response['post'])) {
deserialize_playlist($response['post']);
}
return $response;
});
function deserialize_playlist(&$post) {
if(isset($post->custom_fields->playlist)){
$playlists = $post->custom_fields->playlist;
//custom fields appear to always be returned as an array
foreach($playlists as &$playlist){
$playlist = unserialize($playlist);
}
$post->custom_fields->playlist = $playlists;
}
}
Upvotes: 2