Support » Developing with WordPress » replace create_function for PHP 8

  • Resolved eagerbob

    (@eagerbob)


    Hello all,

    A website that I installed long ago has stopped working. Turns out that the theme uses create_function that has been removed from PHP 8.0
    I looked it up and there are examples of how to rewrite the code, but I can’t get it to work.

    This is the offender:

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

    So how could this line be re-written so that it works with PHP 8.0 ?

    Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Yes this problem has occurred because this function has been removed from PHP 8,this problem solution is

    function fn_Zilla_Twitter_Widget() {
    register_widget( ‘Zilla_Twitter_Widget’ );
    }
    add_action( ‘widgets_init’, ‘fn_Zilla_Twitter_Widget’);

    Thread Starter eagerbob

    (@eagerbob)

    OK Thanks Hiren for your quick response.

    Unfortunately I still get an error:

    `Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-widget-factory.php on line 62 and at least 2 expected in /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-widget.php:163 Stack trace: #0 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-widget-factory.php(62): WP_Widget->__construct() #1 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/widgets.php(115): WP_Widget_Factory->register(‘zilla_FLICKR_Wi…’) #2 /Users/eagerbob/Local Sites/cinetone/app/public/wp-content/themes/cinetone/includes/widgets/widget-flickr.php(21): register_widget(‘zilla_FLICKR_Wi…’) #3 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-hook.php(308): zilla_flickr_widgets(”) #4 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) #5 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #6 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/widgets.php(1854): do_action(‘widgets_init’) #7 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-hook.php(308): wp_widgets_init(”) #8 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-hook.php(332): WP_Hook->apply_filters(NULL, Array) #9 /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/plugin.php(517): WP_Hook->do_action(Array) #10 /Users/eagerbob/Local Sites/cinetone/app/public/wp-settings.php(617): do_action(‘init’) #11 /Users/eagerbob/Local Sites/cinetone/app/public/wp-config.php(91): require_once(‘/Users/eagerbob’) #12 /Users/eagerbob/Local Sites/cinetone/app/public/wp-load.php(50): require_once(‘/Users/eagerbob’) #13 /Users/eagerbob/Local Sites/cinetone/app/public/wp-admin/admin.php(34): require_once(‘/Users/eagerbob’) #14 /Users/eagerbob/Local Sites/cinetone/app/public/wp-admin/index.php(10): require_once(‘/Users/eagerbob’) #15 {main} thrown in /Users/eagerbob/Local Sites/cinetone/app/public/wp-includes/class-wp-widget.php on line 163′

    Seems I’ll have to dive a bit deeper for this.

    Found some info on Stackexchange: https://wordpress.stackexchange.com/questions/383777/too-few-arguments-to-function-wp-widget-construct/383778#383778

    Thanks again

    • This reply was modified 1 week ago by eagerbob. Reason: redacted code
    Hiren Bhanderi

    (@hirenbhanderi)

    yes one little changes in this code you can change one function name.
    Zilla_Twitter_Widget() to __construct()

    Thread Starter eagerbob

    (@eagerbob)

    Ok I changed the code following the information on developer.wordpress but I kept getting errors. Like a “unmatched “}” at the end of my file. Which seems easy to fix but wasn’t.

    I “Solved” it by removing the widgets alltogether. It was a Flickr and a twitter widget, both are not used.

    Thanks

Viewing 4 replies - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.