Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deprecated Widget Constructors
<?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
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
<?php
/**
* Deprecated 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
);
}
/**
* Outputs the content of the widget
*
* @param array $args
* @param array $instance
*/
public function widget( $args, $instance ) {
// outputs the content of the widget
}
/**
* Outputs the options form on admin
*
* @param array $instance The widget options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processing widget options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*/
public function update( $new_instance, $old_instance ) {
// processes widget options to be saved
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.