2

I have nav_menu_options in my Wordpress database as below.

284,'nav_menu_options','a:2:{i:0;b:0;s:8:\"auto_add\";a:0:{}}'

I tried to deserialize this value but the result is empty string - I put a code snippet in <my theme>/function.php as below

echo 'TRY #1'.'<br>';
$v = 'a:2:{i:0;b:0;s:8:\"auto_add\";a:0:{}}';
$d = unserialize($v);
print_r($d);
echo 'TRY #1 end'.'<br>';

echo 'TRY #2'.'<br>';
$v ='a:5:{s:9:"engine_id";a:1:{i:0;s:9:"300000225";}s:15:"transmission_id";a:1:{i:0;s:6:"257691";}s:5:"plant";a:1:{i:0;s:23:"Oshawa, Ontario, Canada";}s:15:"Manufactured in";a:1:{i:0;s:6:"CANADA";}s:22:"Production Seq. Number";a:1:{i:0;s:6:"151411";}}';
$d = unserialize($v);
print_r($d);
echo 'TRY #2 end'.'<br>';

The outcome of the above code is as below.

@1 the deserialized Worpdress value results as empty.

@2 a sample valid serialized value results as readable array.

enter image description here

So my question is Why can't we deserialize Wordpress serialized values? - this is also posted on wordpress.org here.

p.s.

1) More study on Wordpress code shows that we can "decode" the discussed value by using get_option('nav_menu_options'); command.

2) The value of nav_menu_options is the real data copied from database

enter image description here

4
  • What was the real data for the nav_menu_options field? Perhaps, problem is there. Better edit your question and add the real data first to prove that, your real data is serialized correctly so the unserialization should work accordingly. Commented Jul 27, 2015 at 6:50
  • I put in the real data for nav_menu_options already @Mayeenul Islam. Updated on my OP.
    – Nam G VU
    Commented Jul 27, 2015 at 6:53
  • :) It's not the real data. It's already serialized. What's the real data, that's serialized into this: a:2:{i:0;b:0;s:8:\"auto_add\";a:0:{}}? Commented Jul 27, 2015 at 7:34
  • @MayeenulIslam Well, Wordpress do the serialization for the data so I have no idea why we need to prove the real data is correct!?
    – Nam G VU
    Commented Jul 27, 2015 at 11:22

2 Answers 2

7

Your problem is that serialized strings contains escape slashes that are not evaluated as such, because the wrapping quote is a single quote.

You are using:

$v = 'a:2:{i:0;b:0;s:8:\"auto_add\";a:0:{}}'; // wrong

You have to use either

$v = "a:2:{i:0;b:0;s:8:\"auto_add\";a:0:{}}"; // ok

or

$v = 'a:2:{i:0;b:0;s:8:"auto_add";a:0:{}}'; // ok

By the way, you should never manually unserialize values in WordPress database.

When you need to get an option (or a metadata, or anything that may be serialized) use WP functions: they unserialize the value when needed.

E.g. for options, use get_option.

If you are interested in how WordPress do unserialization see maybe_unserialize and is_serialized.

3
  • 1
    "By the way, you should never manually unserialize values in WordPress database" --- I've had to unserialize frequently to access the inside of a user's value. Like, a plugin may store all the page visits of a user in an array within a usermeta. The only way i know how to loop through that data is through manually unserialize the value.
    – Rocky Kev
    Commented Jan 9, 2019 at 18:54
  • @RockyKev unserialize values is ok. You can use maybe_unserialize to obtain the unserialized value... when I say "maually" I mean messing with the string directly
    – gmazzap
    Commented Jan 10, 2019 at 8:27
  • @gmazzap I have tried everything to unserialize the meta doesn't work :'( somehow get_post_meta with the single=true is working but that results in huge amount of query ! which I can't waste is there another solution ! note I tried maybe_unserialize returned the same meta value as string !
    – sdx11
    Commented Mar 23, 2019 at 5:41
0

Its a very old issue but I recently faced similar issue. I was using the multi image upload plugin https://wordpress.org/plugins/multi-image-upload/ and I had Godaddy backups setup. I recently needed to restore my website from godaddy using its backup. and all of a sudden the serialized images data that multi image upload stored in post meta, the unserialize wasn't working on that. After banging my head around it for almost 2 days, I found that instead of using the "Restore" function to restore db, i needed to downloaded the sql backup, and through phpmyadmin import it and all went well. Seems like the restore function was corrupting the serialized data. May help someone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.