ベーシックなコンテンツをウィジェットで追加できる様にする

Ads

Code

<?php
class TextWidgetClass extends WP_Widget {
function TextWidgetClass() {
parent::WP_Widget(false, $name = 'ウィジェットの名前');
}
function widget($args, $instance) {
extract( $args );
$title = apply_filters( 'widget_title', $instance['title'] );
$body = apply_filters( 'widget_body', $instance['body'] );
$the_class = apply_filters( 'widget_body', $instance['the_class'] );
?>
<li <?php echo 'id=""' . "class='text_widget_class $the_class'"; ?> >
<?php if ( $title ) ?>
<?php echo $before_title . $title . $after_title; ?>
<?php echo '<p>' . $body . '</p>'; ?>
</li>
<?php
}
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['body'] = trim($new_instance['body']);
$instance['the_class'] = strip_tags($new_instance['the_class']);
return $instance;
}
function form($instance) {
$title = esc_attr($instance['title']);
$body = esc_attr($instance['body']);
$the_class= esc_attr($instance['the_class']);
?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('サイトに表示されるタイトル:'); ?></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('the_class'); ?>"><?php _e('追加されるclass名:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('the_class'); ?>" name="<?php echo $this->get_field_name('the_class'); ?>" type="text" value="<?php echo $the_class; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('body'); ?>"><?php _e('サイトに表示されるコンテンツ:'); ?></label>
<textarea class="widefat" rows="16" colls="20" id="<?php echo $this->get_field_id('body'); ?>" name="<?php echo $this->get_field_name('body'); ?>"><?php echo $body; ?></textarea>
</p>
<?php
}
}
add_action('widgets_init', create_function('', 'return register_widget("TextWidgetClass");'));
?>
view raw gistfile1.aw hosted with ❤ by GitHub

Screen shot

Note

Description functions.phpに追記。タイトルとコンテンツを表示するだけのシンプルなウィジェットを追加する
WordPress Ver. 3.3.1
Via WordPress Text Widget with Class