On this page
maybe_serialize( string|array|object $data ): mixed
Serializes data, if needed.
Parameters
$datastring|array|object Required-
Data that might be serialized.
Return
mixed A scalar data.
More Information
- Data might need to be serialized to allow it to be successfully stored and retrieved from a database in a form that PHP can understand.
- Confusingly, strings that contain already serialized values are serialized again, resulting in a nested serialization. Other strings are unmodified.
- A possible solution to prevent nested serialization is to check if a variable is serialized using is_serialized()
if( !is_serialized( $data ) ) { $data = maybe_serialize($data); }
Source
File: wp-includes/functions.php. View all references
function maybe_serialize( $data ) {
if ( is_array( $data ) || is_object( $data ) ) {
return serialize( $data );
}
/*
* Double serialization is required for backward compatibility.
* See https://core.trac.wordpress.org/ticket/12930
* Also the world will end. See WP 3.6.1.
*/
if ( is_serialized( $data, false ) ) {
return serialize( $data );
}
return $data;
}
Related
Uses
| Uses | Description |
|---|---|
| is_serialized() wp-includes/functions.php | Checks value to find if it was serialized. |
Used By
| Used By | Description |
|---|---|
| WP_Customize_Manager::import_theme_starter_content() wp-includes/class-wp-customize-manager.php | Imports theme starter content into the customized state. |
| update_network_option() wp-includes/option.php | Updates the value of a network option that was already added. |
| add_network_option() wp-includes/option.php | Adds a new network option. |
| update_usermeta() wp-includes/deprecated.php | Update metadata of user. |
| update_option() wp-includes/option.php | Updates the value of an option that was already added. |
| add_option() wp-includes/option.php | Adds a new option. |
| delete_metadata() wp-includes/meta.php | Deletes metadata for the specified object. |
| update_metadata_by_mid() wp-includes/meta.php | Updates metadata by meta ID. |
| add_metadata() wp-includes/meta.php | Adds metadata for the specified object. |
| update_metadata() wp-includes/meta.php | Updates metadata for the specified object. If no value already exists for the specified object ID and metadata key, the metadata will be added. |
Changelog
| Version | Description |
|---|---|
| 2.0.5 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/maybe_serialize