tommcfarlin/WordPress-Widget-Boilerplate

create_function is deprecated as of PHP 7.2

Closed this issue ยท 12 comments

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

Should do this instead.

function sth_register_widgets() {
	register_widget( 'Widget_Name' );
}
add_action( 'widgets_init', 'sth_register_widgets' );

// OR:

add_action( 'widgets_init', function() { register_widget( 'Widget_Name' ); } );

@andrejcremoznik any idea how to implement this?

@justinpicard I don't follow. Working example is in the comment above in case you missed it.

@andrejcremoznik If i replace the deprecated line with your piece of code I get:
Parse error: syntax error, unexpected ';' in [local path to my widget file] on line 260

So the nested register_widget needs to call the correct class definition. I changed that to Widget_Name to match the boilerplate code.

As for your syntax error, you really should be able to figure it out yourself. There's nothing wrong with my boilerplate code above. Maybe you're misplacing it.

Thanks! I figured it out. Now the widget is showing without any errors in the backend :-)

Just to clarify the above, as I had someone link here asking what's up, the above will work, with the addition of function:

function sth_register_widgets() { register_widget( 'Widget_Name' ); } add_action( 'widgets_init', 'sth_register_widgets' );

However you can also use anonymous function. Often so much easier, shorter and do need any function there need namespace, memory space.

like this example:

add_action( 
    'widgets_init', 
    function () { 
        return register_widget( 'Widget_Name' );
    }, 
    10
);

public static function registerWidget($widgetName){

	add_action('widgets_init', create_function('', 'return register_widget("'.$widgetName.'");'));

}

Deprecated: Function create_function() is deprecated in C:\wamp64\www\pro\wp-content\plugins\revslider\includes\framework\functions-wordpress.class.php

@riyazie yes, this is the topic of the issue and you fund more as one solutions for this problem, maybe my solid short one above. #38 (comment)

will a version with this fix be released?

Please see the branch develop, there have a solution for this topic.
https://github.com/tommcfarlin/WordPress-Widget-Boilerplate/tree/develop