admin管理员组

文章数量:1293512

I've made two custom classes:

  • FooBar
  • BarSchizzle

I'm making a quite custom calculation of a bunch of things, that I would like to save in an array in a post_meta field.

This is how I save it

$obj1 = new FooBar();
$obj2 = new BarSchizzle();
$obj3 = new FooBar();

$arr = [
  'a_key' => $obj1,
  'another_key' => $obj2,
  'a_third_key' => $obj3,
];

update_post_meta( $post_ID, 'my_custom_field', $arr );

// I also tried this, with same result
// update_post_meta( $post_ID, 'my_custom_field', serialize( $arr ) );

This is how I retrieve the array

$initial_arr = get_post_meta( $post_ID, 'my_custom_field', true );
$arr = unserialize( $initial_arr ); // And this is where the error occurs

I'm getting the error: unserialize() [function.unserialize]: Error at offset ... (you can read more about it here).


My theory is, that WordPress hasn't gotten to loading (requiring) my custom classes, when it get's (and unserializes).

I've required the classes by simply slapping them into the functions.php-file, in the top, like this:

require_once( __DIR__ . '/classes/FooBar.php' );
require_once( __DIR__ . '/classes/BarSchizzle.php' );

... Not inside any hook or anything. It felt a bit sloppy/bad, but it has been working perfectly up until now.


So this leaves me with two questions:

  • Do I need to put my require of my custom classes inside a hook, like init or something?
  • Are there another explanation why WordPress stumbles when it tries to unserialize that $arr, containing my custom objects?

I've made two custom classes:

  • FooBar
  • BarSchizzle

I'm making a quite custom calculation of a bunch of things, that I would like to save in an array in a post_meta field.

This is how I save it

$obj1 = new FooBar();
$obj2 = new BarSchizzle();
$obj3 = new FooBar();

$arr = [
  'a_key' => $obj1,
  'another_key' => $obj2,
  'a_third_key' => $obj3,
];

update_post_meta( $post_ID, 'my_custom_field', $arr );

// I also tried this, with same result
// update_post_meta( $post_ID, 'my_custom_field', serialize( $arr ) );

This is how I retrieve the array

$initial_arr = get_post_meta( $post_ID, 'my_custom_field', true );
$arr = unserialize( $initial_arr ); // And this is where the error occurs

I'm getting the error: unserialize() [function.unserialize]: Error at offset ... (you can read more about it here).


My theory is, that WordPress hasn't gotten to loading (requiring) my custom classes, when it get's (and unserializes).

I've required the classes by simply slapping them into the functions.php-file, in the top, like this:

require_once( __DIR__ . '/classes/FooBar.php' );
require_once( __DIR__ . '/classes/BarSchizzle.php' );

... Not inside any hook or anything. It felt a bit sloppy/bad, but it has been working perfectly up until now.


So this leaves me with two questions:

  • Do I need to put my require of my custom classes inside a hook, like init or something?
  • Are there another explanation why WordPress stumbles when it tries to unserialize that $arr, containing my custom objects?
Share Improve this question asked May 2, 2021 at 20:40 ZethZeth 9282 gold badges13 silver badges43 bronze badges 1
  • PHP serialising is a security problem and a well known attack vector. You should not be storing structured data inside post meta. Break it up into multiple key names. Remember, meta keys are not unique, you can save multiple values with the same key. Serialised data can't be queried properly via meta_query, causes fatal errors if you do a search replace query that changes the length of a string, and are difficult to debug – Tom J Nowell Commented May 2, 2021 at 23:53
Add a comment  | 

1 Answer 1

Reset to default 0

Arrays and objects get automatically serialized/unserialized because they are php data types being stored as strings by MySQL. If you save array with update_post_meta you don't need to unserialize the returning value get_post_meta, function will do it for you.

Easier to look at update_post_meta function's source code. It calls update_metadata function where you can see a call of this function:

maybe_serialize( $meta_value );

As you can see, if data is array or an object type - it will serialize this data.

function maybe_serialize( $data ) {
    if ( is_array( $data ) || is_object( $data ) ) {
        return serialize( $data );
}

From the other side, when you use get_post_meta function, this chain is bigger, but at the end it calls maybe_unserialize function, which do unserialization if needed.

maybe_unserialize( string $data )

In your case you are trying unserialize unserializable string. serialize() function returns false and generates E_NOTICE if it can't unserialize a value.

本文标签: functionsHow to save custom made object in an array in a post meta field