jeffimperial
jeffimperial

Reputation: 73

Replace create_function with empty $args for PHP 8

I'm having trouble rewriting this line of code that contains create_function() in preparation for updating to PHP 8+.

The line of code to replace is:

add_action('widgets_init', create_function('', 'return register_widget("Ctxt_Menu_Widget");'));

I am trying to replace it with:

$callback = function(){
    ('', 'return register_widget("Ctxt_Menu_Widget");');
}
add_action('widgets_init', $callback);

But obviously, this isn't right, the replacement code won't allow the array withing that function.

Can someone help me rewrite this? Thanks so much!

Upvotes: 1

Views: 343

Answers (2)

Daniele Continenza
Daniele Continenza

Reputation: 92

The function should be a real (anonimous) function, sintactically valid in php, so it should be something like that

$callback = function(){
    return register_widget("Ctxt_Menu_Widget");
}

Then you can call this function

add_action('widgets_init', $callback);

Now wehn called, it is executed as a normal php functions

Upvotes: 1

jeffimperial
jeffimperial

Reputation: 73

What turns out to be a good solution was:

add_action ( 'widgets_init', 'Ctxt_init_Menu_Widget' );
    function Ctxt_init_Menu_Widget() {
    return register_widget('Ctxt_Menu_Widget');
}

to replace:

add_action('widgets_init', create_function('', 'return register_widget("Ctxt_init_Menu_Widget");'));

Source: https://sarah-moyer.com/fix-create_function-deprecated-in-wordpress/

Upvotes: -1

Related Questions