aubreypwd

Aubrey Portwood

Problems Using WordPress update_option/get_option with PHP serialize?

| Permalink

Are you getting the error…

“unserialize() expects parameter 1 to be string”

…when using WordPress’ get_option function to get serialized data right after the file just saved serialized data using update_option? So was I using, basically, this same type of script:

<?php

$array[]="foo";
$array[]="bar";

update_option('foo',serialize($array));

//here is where I would get the error.
$data = unserialize(get_option('foo'));

?>

So, I had a problem. So, I tried encoding the string to try and speed up the saving time. Using PHP’ base64_(encode/decode) to encode/decode the serialized sting, it has been saving with no problems.

<?php
$array[]="foo";
$array[]="bar";

update_option('foo',base64_encode(serialize($array)));

//No more error?
$data = unserialize(base64_decode(get_option('foo')));
?>