Cannot unserialize WordPress serialized values in `wp_options` table?

I have nav_menu_options in my WordPress database as below.


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);
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);
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 here.


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

Solutions Collecting From Web of "Cannot unserialize WordPress serialized values in `wp_options` table?"

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


$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.