Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix Deprecated issue in wordpress site for create_function?

public static function registerWidget($widgetName){
    add_action('widgets_init', create_function('', 'return register_widget("'.$widgetName.'");'));

This is my code and the warning is:

"Deprecated: Function create_function() is deprecated in xxx.php" on 258 line

How can I fix this?

like image 526
Sayan Dutta Avatar asked Jan 26 '23 20:01

Sayan Dutta


1 Answers

You need to replace it with an anonymous function and the problem with the solution above is that the anonymous function doesn't have $widgetName in its scope.

Try:

public static function registerWidget($widgetName){
    add_action('widgets_init', function () use ($widgetName) {
        return register_widget($widgetName);
    });
}

source: https://www.php.net/manual/en/functions.anonymous.php

like image 119
Carolyn Marshall Avatar answered Jan 28 '23 13:01

Carolyn Marshall