WP_Widget is deprecated since version 4.3.0! Use __construct()

GigaBot

Administrator
You might have seen this Notice in a last few days, if you have wp_debug set to true in your wp-config.php file.

What does it mean?
It means that from 4.3.0 release of WordPress WP_Widget is deprecated and that from now on we must use __construct instead. This is not something related to WP, but to PHP5...but I don't wanna bother you with it ...

Anyway all the WP Developers are informed about this change, but not all of them updated their Plugins and Widgets so far.

If you want to remove this Notice you can either set wp_debug to false or do this change by your self, of course if you are familiar with messing inside WP Files or wait for Developers of such plugins/widgets to update codes.

You should find:

Code:
$this->WP_Widget

and change that to:

Code:
parent::__construct

If you are familiar with Notepad++ you can easily do it with search >> find >> find in files ...and then replace in files.

You should look in all files in plugins folder and your themes folder...

But DON'T do it if you are not familiar with such actions.
 

ogah

New member
usually i use like this code to make widget in my plugins
PHP:
<?php
##### RECENT SIDEBAR #####
class Recent extends WP_Widget { 
	function __construct() { 
		$widget_ops = array('classname' => 'widget_recent', 'description' => __( "Recent Search on Sidebar") ); 

		parent::__construct('recent', __('Recent Search'), $widget_ops); 
	} 

	function widget( $args, $instance ) { 
		extract($args); 
		$title = apply_filters('widget_title', empty($instance['title']) ? __('Recent Search') : $instance['title'], $instance, $this->id_base); 
		echo $before_widget; 
		if ( $title ) 
			echo $before_title . $title . $after_title; 
		include('recent.php'); 
		echo $after_widget; 
	} 

	function update( $new_instance, $old_instance ) { 
		$instance = $old_instance; 
		$instance['title'] = strip_tags($new_instance['title']); 
		return $instance; 
	} 

	function form( $instance ) { 
		$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) ); 
		$title = strip_tags($instance['title']); 
?> 
			<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> 
<?php
	} 
}

function recentside() { 
	register_widget('Recent'); 
} 
add_action('widgets_init', 'recentside'); 
##### END RECENT #####
 

GigaBot

Administrator
This is happening in last 5-6 days since 4.0.3 WordPress Release, now WP_Widget is deprecated, and many Widgets/Plugins and Themes used WP_Widget ... now if those Widgets/Plugins and Themes developers didn't update them with __construct replacements ...people will get that Notice ...that is why I posted it, so people won't be worried about it, because, there wasn't such notice so far.
 

Genesis

Administrator
I'd rather wait for the debug from the theme/plugin author. That would be one good test to see how good they are at looking after their theme/plugin, and a warning if they don't.
 

GigaBot

Administrator
Genesis said:
I'd rather wait for the debug from the theme/plugin author. That would be one good test to see how good they are at looking after their theme/plugin, and a warning if they don't.

You're absolutely right about that... if WordPress inform them that they should do some important change and they wait to do that, you should be concern about their works too.

p.s. I did all the necessary changes right away and I can tell you that number of downloads of my WordPress hosted Plugins increases in a last few days ...
 
I wouldn't go playing with __construct. Its a security issue if not implemented correctly. Its a good way for a hacker to add something if there is no child constructor.
I found this on hacking with php. It may not be a hack, but it does show how there are flaws in it if not used correctly. As stated, I would wait for the authors to update their work.


Here is an example of it here
The fact that PHP always calls the "nearest" constructor, that is if there is no child constructor it will call the parent constructor and not the grandparent constructor, means that we need to call the parent constructor ourselves. We can do this by using the special function call parent::__construct(). The "parent" part means "get the parent of this object, and use it", and the __construct() part means "call the construct function", of course. So the whole line means "get the parent of this object then call its constructor"

As you can see, the call to the parent's __construct() is just a normal function call, and the dog constructor needs a dog name as its parameter. So, to make the poodle class work properly, we would need the following:
Code:
class poodle extends dog {
    public function bark() {
        print "Yip!\n";
    }
    
    public function __construct($DogName) {
        parent::__construct($DogName);
        print "Creating a poodle\n";
    }
}
Try running that - you should get output like to this:
Creating Poppy
Creating a poodle
My name is Poppy. If you find me, please call 555-1234

Note that "Creating Poppy" is output before "Creating a poodle", which might seem backwards, but it makes sense given that we call the dog constructor before we do any poodle code. It is always best to call parent::__construct() first from the constructor of a child class in order to make sure all the parent's variables are set up correctly before you try and set up the new stuff.
 

GigaBot

Administrator
Well, if you sure about that you should post it at WordPress.org Support, because this is the way WordPress core Developers recommend ...