Call to undefined function create_function

Some time when we upgrade the PHP with PHP 8 then getting the below error:

Fatal error: Uncaught Error: Call to undefined function create_function() in public_html/wp-content/themes/my-theme/functions/widgets/widget-categories.php on line 82

There some code from some active theme or pluginis not compatible with the latest version PHP 8 that’s why throwing Fatal error.

We have two solutions to fix this type problem.

  1. We can downgrade the PHP version with PHP 7.4 to slow the problem
  2. We can change the code as below to fix the problem

To fix, Please change below code:

add_action( 'widgets_init', create_function('', 'return register_widget("rdi_randompostwidget");') ); ?>

To this code:

add_action( 'widgets_init', 'rdi_randompostwidget_in_it' ); 
function rdi_randompostwidget_in_it() {
return register_widget('rdi_randompostwidget');
}

Leave a Reply