Sayan Dutta
Sayan Dutta

Reputation: 41

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?

Upvotes: 4

Views: 4502

Answers (1)

Carolyn Marshall
Carolyn Marshall

Reputation: 131

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

Upvotes: 13

Related Questions