class My_Widget extends WP_Widget { // Constructor method function __construct() { parent::__construct( 'my_widget', // Widget ID 'My Widget', // Widget name array( 'description' => 'My widget description' ) // Widget description ); } // Widget output method function widget( $args, $instance ) { // Output widget content here } }
class My_Widget extends WP_Widget { // Constructor method function __construct() { parent::__construct( 'my_widget', // Widget ID 'My Widget', // Widget name array( 'description' => 'My widget description' ) // Widget description ); // Set default widget options $this->defaults = array( 'title' => 'My Widget Title', 'content' => 'My Widget Content' ); } // Widget output method function widget( $args, $instance ) { // Merge user-defined and default options $instance = wp_parse_args( $instance, $this->defaults ); // Output widget content here } }
class My_Widget extends WP_Widget { // Constructor method function __construct() { parent::__construct( 'my_widget', // Widget ID 'My Widget', // Widget name array( 'description' => 'My widget description' ) // Widget description ); } // Widget output method function widget( $args, $instance ) { // Output widget content here } } // Register the widget with WordPress add_action( 'widgets_init', function() { register_widget( 'My_Widget' ); } );In all three examples, the widget __construct method is used to initialize the widget and set default options or register the widget with the WordPress widget system. The package library in this case is the WordPress core.