PHP Fatal error: Uncaught Error: Call to undefined function create_function()

I recently encountered PHP Fatal error: Uncaught Error: Call to undefined function create_function error when updating WordPress install from PHP7.4 to PHP8.0. Below was the line of code that was causing the issue

[php]add_action( ‘widgets_init’, create_function( ”, ‘register_widget( "Qode_Pitch_Sticky_Sidebar" );’ ) );[/php]

In PHP 8, the create_function function has been deprecated, and its use is no longer recommended. Instead, you can use a closure to achieve the same result. Below is the updated code.

PHP Fatal error Fix

[php]add_action(‘widgets_init’, function() {
register_widget(‘Qode_Pitch_Sticky_Sidebar’);
});[/php]

In the above code, an anonymous function (also called a closure) is defined using the function keyword. This function calls register_widget() with the class name Qode_Pitch_Sticky_Sidebar as its argument. The function is then attached to the widgets_init action using add_action(). This updated code should work correctly with PHP 8.