Make WordPress Core

Opened 3 years ago

Last modified 3 years ago

#54355 new defect (bug)

Serialization of 'Closure' is not allowed

Reported by: cambridgeandy's profile cambridgeandy Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.8.1
Component: General Keywords:
Focuses: Cc:



I'm using an object to manage my initialization.

I'm looking at the documentation at

When I register a hook using a closure (callable) that invokes a method on the object I get an error saying that "Serialization of 'Closure' is not allowed thrown at line [599] in file var/www/html/wp-includes/functions.php"

Here is my offending code:

register_activation_hook(WPMP_BASE_FILE_NAME, function() use ($initialize_service) {

Looking at the code in functions.php I can see that there are checks if the data is an array or an object:

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

The issue is that the Closure is seen as an object, but cannot be serialized (see

A potential fix could be:

function maybe_serialize( $data ) {
	if ( is_array( $data ) || is_object( $data ) ) {
		try {
			return serialize( $data );
		} catch (\Throwable $e) {
			return $data;


However, this requires PHP7.0 which would violate the requirement for WP core to support 5.6

Change History (1)

#1 @praem90
3 years ago

PHP can't serialize closure directly. We can use package for this support.

So, it recommended to use something like below may help to fix your problem.

register_activation_hook(WPMP_BASE_FILE_NAME, array($initialize_service, 'activate'))
Note: See TracTickets for help on using tickets.