プラグインの作り方 ウィジェットの追加
//------------------------------------------------------------------------------ // ウィジェットの追加 //------------------------------------------------------------------------------ add_action('widgets_init', 'WP_Widget_thumb_boxInit'); // 詳しいことは以下でという //------------------------------------------------------------------------------ // ウィジェット関数の登録 //------------------------------------------------------------------------------ function WP_Widget_thumb_boxInit() { register_widget('WP_Widget_ImgPostA'); register_widget('WP_Widget_BoxPostA'); register_widget('WP_Widget_CommonPostA'); register_widget('WP_Widget_ImgRankingA'); }この例では、4つのウィジェットを登録している。
ウィジェットのクラス本体の基本形
//============================================================================== class WP_Widget_ImgPostA extends WP_Widget //============================================================================== { //-------------------------------------------------------------------------- // 定義 //-------------------------------------------------------------------------- function WelcartMyBox_widget() { $widget_ops = array( 'classname' => 'WelcartMyBox_widget', // クラス名 とりあえずぶつからない名前 'name' => __('Welcart My Box'), // ウィジェット選択での表示名 'description' => __('Welcart My Box') // ウィジェット選択での説明文 ); // Wiget Option 用 ウィンドサイズ array() とするとデフォルトサイズ $control_ops = array('width' => 400, 'height' => 350); // WigetID , Wiget Title(nameで指定済み) , wiget_ops , control_ops parent::__construct( 'WelcartMyBox_widget', // クラス名 __('Text'), // 決まり $widget_ops, // 表示名等 $control_ops // ウィジェットウィンドウ属性 ); } //-------------------------------------------------------------------------- // 設定値の書き込み //-------------------------------------------------------------------------- function update($new_instance, $old_instance) { // processes widget options to be saved $instance = $old_instance; $instance['title'] = strip_tags($new_instance['title']); $instance['text' ] = $new_instance['text' ]; $instance['templ'] = $new_instance['templ']; $instance['colmn'] = $new_instance['colmn']; $instance['order'] = $new_instance['order']; return $instance; } //-------------------------------------------------------------------------- // 設定フォーム (ウィジェットをサイドバーでの入力フォーム //-------------------------------------------------------------------------- function form($instance) { // --------------------------------------------------------------------- // 設定値 取り込み // --------------------------------------------------------------------- $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'text' => '' ) ); $title = strip_tags( $instance['title']); $text = esc_textarea($instance['text']); $templ = strip_tags( $instance['templ']); $colmn = strip_tags( $instance['colmn']); $order = strip_tags( $instance['order']); ?> // --------------------------------------------------------------------- // 設定フォーム表示 // --------------------------------------------------------------------- <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 esc_attr($title); ?>" /></p> <p><label for="<?php echo $this->get_field_id('title'); ?>"> <?php _e('表示前コメント:'); ?></label> <textarea class="widefat" rows="2" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"> <?php echo $text; ?></textarea></p> <p><label for="<?php echo $this->get_field_id('templ'); ?>"> <?php _e('テンプレート:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('templ'); ?>" name="<?php echo $this->get_field_name('templ'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <p><label for="<?php echo $this->get_field_id('colmn'); ?>"> <?php _e('表示カラム:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('colmn'); ?>" name="<?php echo $this->get_field_name('colmn'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <p><label for="<?php echo $this->get_field_id('order'); ?>"> <?php _e('表示データ:'); ?></label> <input class="widefat" id="<?php echo $this->get_field_id('order'); ?>" name="<?php echo $this->get_field_name('order'); ?>" type="text" value="<?php echo esc_attr($title); ?>" /></p> <?php } //-------------------------------------------------------------------------- // 表示本体 //-------------------------------------------------------------------------- function widget($args, $instance) { //---------------------------------------------------------------------- // 設定値の取り込み //---------------------------------------------------------------------- extract($args); $title = apply_filters( 'widget_title' , empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base ); $text = apply_filters( 'widget_text' , empty( $instance['text'] ) ? '' : $instance['text'] , $instance ); $order = apply_filters( 'widget_order' , empty( $instance['templ'] ) ? '' : $instance['templ'] , $instance ); $templ = apply_filters( 'widget_templ' , empty( $instance['colmn'] ) ? '' : $instance['colmn'] , $instance ); $colmn = apply_filters( 'widget_colmn' , empty( $instance['order'] ) ? '' : $instance['order'] , $instance ); //---------------------------------------------------------------------- // 決まり //---------------------------------------------------------------------- echo $before_widget; if ( !empty( $title ) ) { echo $before_title . $title . $after_title; } ?> <div class="textwidget"><?php echo !empty( $instance['filter'] ) ? wpautop( $text ) : $text; ?></div> <?php //---------------------------------------------------------------------- // 表示ボディ //---------------------------------------------------------------------- echo WelcartMyBox_View($order,$templ,$colmn); //---------------------------------------------------------------------- // 決まり //---------------------------------------------------------------------- echo $after_widget; } }
スポンサーリンク