- Add the following code to the functions.php file of your WordPress theme to register the widget:
function register_custom_widget() {
register_widget( 'custom_widget_class' );
}
add_action( 'widgets_init', 'register_custom_widget' );
- Create a new file named custom-widget.php in your theme directory and add the code for your custom widget class:
class Custom_Widget_Class extends WP_Widget {
// Constructor, widget settings, etc.
}
- Define the widget control and display elements within the custom widget class:
public function __construct() {
parent::__construct(
'custom_widget_id',
'Custom Widget Title',
array( 'description' => 'Custom Widget Description' )
);
}
public function widget( $args, $instance ) {
// Widget output
}
public function form( $instance ) {
// Output for the widget settings in the admin
}
public function update( $new_instance, $old_instance ) {
// Save widget options
}