插件开发示例收藏 Song Widget

    <?php
function wpshout_register_widgets() {
    register_widget( 'Favorite_Song_Widget');
}

add_action( 'widgets_init', 'wpshout_register_widgets' );

class Favorite_Song_Widget extends WP_Widget {

function Favorite_Song_Widget() {
    // Instantiate the parent object
    parent::__construct(
            'favorite_song_widget', // Base ID
            __('Favorite Song', 'text_domain'), // Name
            array( 'description' => __( 'Widget for playable favorite song', 'text_domain' ), ) // Args
    );
}

function widget( $args, $instance ) {
    echo $args['before_widget']; 
    echo '<h3>Favorite Song Lists:</h3>';
    echo $instance['songinfo'];
    echo '<a href="' . $instance['link'] . '">Download it</a><br>';
    echo $instance['description'];
            echo $args['after_widget'];
}

function update($new_abc,$old_abc) {
    $instance = $old_abc;
    // Fields
    $instance['link'] = strip_tags($new_abc['link']);
    $instance['songinfo'] = strip_tags($new_abc['songinfo']);
            $instance['description'] = strip_tags($new_abc['description']);
    return $instance;
}

// Widget form creation
function form($instance) {
     $link = '';
    $songinfo = '';
            $description = '';
    // Check values
    if( $instance) {
        $link = esc_attr($instance['link']);
        $songinfo = esc_textarea($instance['songinfo']);
                    $description = esc_textarea($instance['description']);
    } ?>
     

    <p>
        <label for="<?php echo $this->get_field_id('link'); ?>"><?php _e('Link', 'wp_widget_plugin'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('link'); ?>" name="<?php echo $this->get_field_name('link'); ?>" type="text" value="<?php echo $link; ?>" />
    </p>
     
    <p>
        <label for="<?php echo $this->get_field_id('songinfo'); ?>"><?php _e('Song Info:', 'wp_widget_plugin'); ?></label>
        <input class="widefat" id="<?php echo $this->get_field_id('songinfo'); ?>" name="<?php echo $this->get_field_name('songinfo'); ?>" type="text" value="<?php echo $songinfo; ?>" />
    </p>
            
            <p>
        <label for="<?php echo $this->get_field_id('description'); ?>"><?php _e('Description:', 'wp_widget_plugin'); ?></label>
        <textarea class="widefat" id="<?php echo $this->get_field_id('description'); ?>" name="<?php echo $this->get_field_name('description'); ?>" type="text" value="<?php echo $description; ?>"></textarea>
    </p>
            
            <p><a href="#" id="add-more-tabs"><?php _e('Add More Tabs', 'wp_widget_plugin'); ?></a></p>
    
<?php }

}