skip to Main Content

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!

2

Answers


  1. Chosen as BEST ANSWER

    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/


  2. 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

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search