Search code examples
phpwordpress

Trying to update PHP to 8.2.23, getting error deprecated create_function


Error points to the following line. I can't figure out how to rewrite with anonymous function instead of create_function

add_action( 'widgets_init', create_function( '', 'register_widget( "Open_Table_Widget" );' ) );

I am new to coding and have researched how to fix this, but can't seem to get it right.


Solution

  • create_function() returns a string, the name of the function, and that is what is (apparently) needed by add_action(). An anonymous function doesn't have a name and its name can therefore not be represented in a string.

    However, you can write out the function as a normal function:

    function OpenTableWidgetRegisterAction()
    {
        register_widget("Open_Table_Widget");
    }
    

    And then use:

    add_action("widgets_init", "OpenTableWidgetRegisterAction");
    

    That should work. Just make sure the function name is really unique, so long names are good.

    You would have to show the code of add_action() if you want a way for it to work with anonymous functions, because as far I can tell from your question it requires a function name string.