Comment crée un widget WordPress

Comment crée un widget WordPress

<?php 
/* Fichier simple_widget.php */
class Simple_widget extends WP_Widget {
 
        /* Configuration globale du widget */
        function __construct() {
                $widget_args = array(
                        'classname' => 'widget_simple_test',
                        'description' => 'Un simple widget de test'
                );
 
                $control_args = array(
                        'width' => 450
                );
 
                parent::__construct(
                        'ts_simple_widget',
                        __('Un simple widget de test'),
                        $widget_args,
                        $control_args
                );
        }
 
        // Affichage en front-end
        function widget($args, $instance) {
                extract($args);
                $title = strip_tags($instance['title']);
                $description = $instance['description'];
 
                echo $before_widget;
 
                echo $before_title . $title . $after_title;
 
                echo $description;
 
                echo $after_widget;
        }
 
        /* Sauvegarde + Insertion */
        function update( $new_instance, $old_instance ) {
                $new_instance['title'] = strip_tags($new_instance['title']);
                return $new_instance;
        }
 
        /* Affichage du formulaire de réglages du widget en back-end */
        function form($instance) {
                $instance = wp_parse_args(
                        $instance,
                        array(
                                'title' => 'Titre de test',
                                'description' => ''
                        )
                );
 
                $title = strip_tags($instance['title']);
                $description = $instance['description'];
 
                ?>
 
                <p>
                        <label for="<?php echo $this->get_field_id('title'); ?>">Titre :</label>
                        <input class="" 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>
 
                <p>
                        <label for="<?php echo $this->get_field_id('description'); ?>">Description :</label>
                        <textarea class="" rows="16" col="20" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>"><?php echo esc_textarea($description); ?></textarea>
                </p>
 
 
 
        <?php }
}

/* Sauvegarde du widget */
function init_test_widget() {
        register_widget('Simple_widget');
}
add_action('widgets_init', 'init_test_widget');

 

Documentation du WordPress cliquer ici