Sarah Moyer

WordPress Development for Web Agencies

  • Home
  • Work with Me
    • for Agencies
    • for Designers
    • for Business Owners
    • FAQs
    • Website Care
  • Portfolio
  • Blog
  • Contact

Fix create_function() deprecated PHP function in WordPress

Last updated on September 12, 2020 · Posted in Snippets · errors

If you turn on WP_DEBUG, you may find an error like this:

Deprecated: Function create_function() is deprecated in /wp-content/themes/your-theme/functions/widgets/widget-categories.php on line 106

To fix, change this:

add_action( 'widgets_init', create_function( '', 'return register_widget("Woo_Widget_AdSpace");' ), 1 );

to this:

add_action ( 'widgets_init', 'Woo_Widget_init_AdSpace' );
function Woo_Widget_init_AdSpace() {
return register_widget('Woo_Widget_AdSpace');
}

More info here: https://wordpress.org/support/topic/function-create_function-is-deprecated-in-php-7-2-0/

Warning: Please note that if your plugin or theme is by a third-party who has updates, any updates will remove your fixes. Please notify the plugin author or theme author rather than making these changes yourself.

See also:

  • Fix PHP error “Methods with same name as their class will not be constructors”
  • Fix each() deprecated PHP function in WordPress
  • Fix PHP error “called constructor method for WP_Widget… deprecated”

Was this post helpful?

Comments

  1. Sal Ferrarello says

    March 15, 2019 at 9:06 am

    I’ve been enjoying these posts – thank you for sharing them.

    In this example another option you could use is an anonymous function with something like

    add_action( 'widgets_init', function () {
    return register_widget('Woo_Widget_AdSpace');
    }, 1 );

    Thanks again for posting these.

    Reply
    • Sarah Moyer says

      March 21, 2019 at 3:45 pm

      Thank you, Sal!

      Reply
    • Jan says

      November 11, 2021 at 7:15 am

      Hi Sal and Sarah,
      thanks for these solution tipps.
      But in my case (current WP version with PHP 8) they only produce another error.

      I’ve used:

      add_action(‘widgets_init’, function () {
      return register_widget(‘SimpleSectionNav’);
      }, 1 );

      And in result I get that error message (sorry for spamming):

      Fatal error: Uncaught ArgumentCountError: Too few arguments to function WP_Widget::__construct(), 0 passed in /var/www/sites/wordpress-2017/wp-includes/class-wp-widget-factory.php on line 61 and at least 2 expected in /var/www/sites/wordpress-2017/wp-includes/class-wp-widget.php:162 Stack trace: #0 /var/www/sites/wordpress-2017/wp-includes/class-wp-widget-factory.php(61): WP_Widget->__construct() #1 /var/www/sites/wordpress-2017/wp-includes/widgets.php(115): WP_Widget_Factory->register(‘SimpleSectionNa…’) #2 /var/www/sites/wordpress-2017/wp-content/plugins/simple-section-navigation/simple_section_nav.php(155): register_widget(‘SimpleSectionNa…’) #3 /var/www/sites/wordpress-2017/wp-includes/class-wp-hook.php(303): {closure}(”) #4 /var/www/sites/wordpress-2017/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters(”, Array) #5 /var/www/sites/wordpress-2017/wp-includes/plugin.php(470): WP_Hook->do_action(Array) #6 /var/www/sites/wordpress-2017/wp-includes/widgets.php(1809): do_action(‘widgets_init’) #7 /var/www/sites/wordpress-2017/wp-includes/class-wp-hook.php(303): wp_widgets_init(”) #8 /var/www/sites/wordpress-2017/wp-includes/class-wp-hook.php(327): WP_Hook->apply_filters(NULL, Array) #9 /var/www/sites/wordpress-2017/wp-includes/plugin.php(470): WP_Hook->do_action(Array) #10 /var/www/sites/wordpress-2017/wp-settings.php(578): do_action(‘init’) #11 /var/www/sites/wordpress-2017/wp-config.php(86): require_once(‘/is/htdocs/wp10…’) #12 /var/www/sites/wordpress-2017/wp-load.php(50): require_once(‘/is/htdocs/wp10…’) #13 /var/www/sites/wordpress-2017/wp-blog-header.php(13): require_once(‘/is/htdocs/wp10…’) #14 /var/www/sites/wordpress-2017/index.php(17): require(‘/is/htdocs/wp10…’) #15 {main} thrown in /var/www/sites/wordpress-2017/wp-includes/class-wp-widget.php on line 162

      When removing the add_action(…), the WP works.
      Do you have any hints for me please?
      Thanks and best regards
      Jan

      Reply
      • Sarah Moyer King says

        March 25, 2022 at 7:49 am

        I created this post awhile ago, so it could be an outdated solution. I hope you are able to figure it out!

        Reply
  2. Thomas Kuerten says

    February 2, 2020 at 10:01 am

    Thank you thank you thank you!!!
    You really saved my day, week and month with your helpful examples.

    take care,
    Thomas.

    Reply
    • Sarah Moyer King says

      February 3, 2020 at 12:30 pm

      You are SO welcome! It saved my day to figure out the solution! So happy to help others 🙂

      Blessings,
      Sarah

      Reply
  3. Pat says

    June 4, 2021 at 3:57 pm

    Thank you so much for this – I’m not an expert coder but I can follow instruction with an example and this was perfect. My debug log is empty!

    Proof that 65 year old grannies can still amaze themselves – with a little help from some generous souls x 🙂

    Reply
    • Sarah Moyer King says

      June 26, 2021 at 1:22 pm

      That’s wonderful, Pat! Glad to hear that my work paid off for someone else. I love helping your age bracket 🙂

      Reply
      • Steve says

        September 30, 2021 at 11:45 am

        Some would call me a 65-year-old as well, but I prefer to think of myself as 50 (plus shipping and handling)

        Reply
        • Sarah Moyer King says

          March 25, 2022 at 7:41 am

          You make me smile!

          Reply
    • Russell says

      December 1, 2021 at 6:49 pm

      This comment made me smile! Stay blessed! 🙂

      Reply
  4. Lin Hai says

    June 28, 2021 at 8:27 pm

    Thank you! I have fixed my theme problem.

    Reply
    • Sarah Moyer King says

      July 26, 2021 at 1:35 pm

      wonderful!

      Reply
  5. Rahul Gupta says

    October 6, 2021 at 2:26 am

    This line shows Function create_function() is deprecated erron and I don’t know how to fix it –add_filter( ‘pre_transient_update_core’, create_function( ‘$a’, “return null;” ) );

    Reply
    • Sarah Moyer King says

      March 25, 2022 at 7:42 am

      Help, anyone? I don’t know.

      Reply
  6. Gene Steinberg says

    December 14, 2021 at 3:17 pm

    I have an issue with a PHP function for a now-discontinued theme, Headway, that puts up an error in PHP 7.4.x, and breaks the site in PHP 8.x.

    Deprecated: Function create_function() is deprecated in /var/www/vhosts/ourstrangeplanet.com/public_html/wp-content/themes/headway/library/api/api-admin-meta-box.php on line 29

    Can someone help me fix it?

    Gene Steinberg

    Reply
  7. Bruno says

    April 4, 2022 at 11:21 am

    I would like to thank you so much ! My Hosting provider did an update of PHP without me to understand the consequences on my WordPress site.

    Because of your post, it took only 10 minutes for me to correct the php of the old WordPress theme I was using. You are a life saver !

    Reply
    • Sarah Moyer King says

      May 13, 2022 at 6:44 pm

      Wonderful! Glad to help!

      Reply
  8. Antony says

    December 19, 2022 at 10:37 am

    Deprecated: Function create_function() is deprecated in /home/portklm/public_html/wp-includes/plugin.php on line 437

    how i can solve this issues

    Reply

Trackbacks

  1. Fix PHP error "Methods with same name as their class will not be constructors" - Sarah Moyer says:
    May 8, 2019 at 3:00 pm

    […] Fix create_function() deprecated PHP function in WordPress […]

    Reply
  2. Fix PHP error "called constructor method for WP_Widget... deprecated" - Sarah Moyer says:
    May 8, 2019 at 3:01 pm

    […] Fix create_function() deprecated PHP function in WordPress […]

    Reply
  3. Fix each() deprecated PHP function in WordPress - Sarah Moyer says:
    November 21, 2019 at 10:05 am

    […] Fix create_function() deprecated PHP function in WordPress […]

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Comments Protected by WP-SpamShield Anti-Spam

Recommended Resources

Copyright © 2025 by Sarah Moyer · Privacy Policy