Joshua David Nelson

Bullshit Free WordPress Development

  • Services
  • Code
  • About
  • Contact

Fixing Your Deprecated Widget Constructors in WordPress 4.3

August 19, 2015 by Joshua Nelson

As of WordPress 4.3 older versions of constructor functions are deprecated. Typically these are common in widgets, where you’re extending the parent WP_Widget class. I’ve recently updated a few of my older versions in my plugins and on client site’s, here’s the basics.

Older, now depreciated code (truncated to the first 20 lines):

<?php
/**
* Depreciated Method as of WordPress 4.3
* @see https://make.wordpress.org/core/2015/07/02/deprecating-php4-style-constructors-in-wordpress-4-3/
* @see http://codex.wordpress.org/Widgets_API
*/
class My_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function My_Widget() {
$widget_args = array( 'description' => __( 'A Foo Widget', 'text_domain' ) );
$this->WP_Widget(
'foo_widget', // Base ID
__( 'Widget Title', 'text_domain' ), // Name
$widget_args // Args
);
}
view raw depreciated-method.php hosted with ❤ by GitHub

Harder, better, faster, stronger: *

<?php
/**
* Correct method for WordPress 4.3
* @see https://make.wordpress.org/core/2015/07/02/deprecating-php4-style-constructors-in-wordpress-4-3/
* @see http://codex.wordpress.org/Widgets_API
*/
class My_Widget extends WP_Widget {
/**
* Sets up the widgets name etc
*/
public function __construct() {
$widget_args = array( 'description' => __( 'A Foo Widget', 'text_domain' ) );
parent::__construct(
'foo_widget', // Base ID
__( 'Widget Title', 'text_domain' ), // Name
$widget_args // Args
);
}
view raw correct-method.php hosted with ❤ by GitHub

It breaks down to being an older PHP method – naming the constructor function the same as the class and/or calling the parent constructor via the class name. Newer versions of PHP allow us to call parent classes via parent:: and utilize the __construct() function, which is now the preferred method for WordPress.

For more information, check out the WP Core Blog post on the subject and the Widgets API Documentation.

Filed Under: WordPress Tagged With: deprecated, php, widgets

Discussion

  1. Grant Norwood says

    October 9, 2015 at 03:29

    Fyi, typo in your first sentence. Should be “constructor functions are deprecated” 🙂

    • Joshua Nelson says

      October 9, 2015 at 04:18

      Ah, nice find, thanks for the heads up – I’ve updated it accordingly.

  • Twitter
  • RSS Feed URL

About Me

I'm a WordPress Engineer. I build sleek, custom websites with WordPress and Genesis.

See my services and my recent work.

Contact me to get your project started.

Gravity Forms Plugin for WordPress Fastest WordPress Hosting

Recent Posts

  • Using Font Awesome Icons for WooCommerce Grid / List Toggle
  • Disable Blog: WordPress Gone Blog-less
  • Category (Taxonomy) Dropdown Filtered By Post Type
  • Weather in WordPress with Dark Sky
  • Fixing Your Deprecated Widget Constructors in WordPress 4.3
  • Twitter
  • RSS Feed URL
  • Code Snippets
  • My Plugins
  • Make a Payment

© Joshua David Nelson | Hand-Forged | WordPress + Genesis | Terms of Service | Legal | Contact