自作ウィジェット作成について
http://kachibito.net/wordpress/custom/how-to-add-your-widget.html
class MyWidgetItem extends WP_Widget {
function MyWidgetItem() {
parent::WP_Widget(false, $name = 'リンク先バナー');
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$url = apply_filters( 'widget_url', $instance['url'] );
$img = apply_filters( 'widget_img', $instance['img'] );
?>
<li <?php echo 'class="banner"'; ?> >
<a href="<?php echo $url; ?>" target="_blank"><img src="<?php echo $img; ?>"></a>
</li>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['url'] = strip_tags($new_instance['url']);
$instance['img'] = strip_tags($new_instance['img']);
return $instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
$url = esc_attr($instance['url']);
$img = esc_attr($instance['img']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>">
<?php _e('Title:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo $title; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">
<?php _e('URL:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" type="text" value="<?php echo $url; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('img'); ?>">
<?php _e('img:'); ?>
</label>
<input class="widefat" id="<?php echo $this->get_field_id('img'); ?>" name="<?php echo $this->get_field_name('img'); ?>" type="text" value="<?php echo $img; ?>" />
</p>
<?php if ( $img ) :?>
<img src="<?php echo $img; ?>">
<?php endif;?>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("MyWidgetItem");'));