Languages: English • 日本語 (Add your language)
Serialize data, if needed.
<?php maybe_serialize( $data ); ?>
<?php // Strings are returned untouched. $data = 'Hello World!'; echo maybe_serialize( $data ); // Hello World! // Integers, floats and boolean values are also returned untouched. $data = 55; echo maybe_serialize( $data ); // 55 $data = 4.560 echo maybe_serialize( $data ); // 4.560 $data = true; $data = maybe_serialize( $data ); // $data = true; $data = null; $data = maybe_serialize( $data ); // $data = null // An array or object will be returned as a serialized string. $data = array( 1 => 'Hello World!', 'foo' => 'bar' ); echo maybe_serialize( $data ); // a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";} // A serialized string will be serialized again. $data = 'a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";}'; echo maybe_serialize( $data ); // s:50:"a:2:{i:1;s:12:"Hello World!";s:3:"foo";s:3:"bar";}"; ?>
A possible solution to prevent nested serialization is to check if a variable is serialized using
<?php if(!is_serialized( $data )) { $data = maybe_serialize($data); } ?>
More info at is_serialized().
Since: 2.0.5
maybe_serialize() is located in wp-includes/functions.php
.