ウィジェットの作り方

2.8から便利度アップ

ウィジェット機能は、WordPress 2.2 から実装されています。 バージョン2.8 から実装方法が変更される予定です。

オブジェクト指向

WordPress2.8以降では、ウィジェット作成をオブジェクト指向で行うようになりました。WP_Widget オブジェクトを継承することにより、個々のウィジェット作成を行います。 以下では、wp-includes/default-widgets.phpの Archives widget class を例に取り上げます。(左端の数字は行番号)

215    class WP_Widget_Archives extends WP_Widget {

コンストラクタ

コンストラクタ(インスタンス生成時に実行)で WP_Widget メソッドを実行します。WP_Widget の引数は、ID、ウィジェット名、ウィジェットオプション、コントロールオプション(省略可)です。 ウィジェットオプションは連想配列(classname と description)、コントロールオプションは連想配列(width と height)です。

217  	function WP_Widget_Archives() {
218          $widget_ops = array('classname' => 'widget_archive', 'description' => __( 'A monthly archive of your blog’s posts') );
219          $this->WP_Widget('archives', __('Archives'), $widget_ops);
220      }

※ PHP4との互換性のため、コンストラクタはクラス名と同名の関数になっています。

ウィジェット表示

ウィジェット表示は、widget メソッドを使用します。 $argsは、$before_widget, $after_widget, $before_title, $after_title が格納されています。これらはテーマテンプレートの functions.php で設定されています。 $instance は、個々のウィジェットの設定パラメータが格納されています。

222  	function widget( $args, $instance ) {
223          extract($args);
224          $c = $instance['count'] ? '1' : '0';
225          $d = $instance['dropdown'] ? '1' : '0';
226          $title = apply_filters('widget_title', empty($instance['title']) ? __('Archives') : $instance['title']);
227  
228          echo $before_widget;
(省略)

設定の更新

(管理画面で行う)設定の更新は、update メソッドを使用します。 テキスト入力については、タグ除去(strip_tags)を行うようにしてください。

247  	function update( $new_instance, $old_instance ) {
248          $instance = $old_instance;
249          $new_instance = wp_parse_args( (array) $new_instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
250          $instance['title'] = strip_tags($new_instance['title']);
251          $instance['count'] = $new_instance['count'] ? 1 : 0;
252          $instance['dropdown'] = $new_instance['dropdown'] ? 1 : 0;
253  
254          return $instance;
255      }

管理画面のフォーム作成

管理画面でウィジェット設定フォームを作成するには、form メソッドを使用します。 get_field_id()get_field_name() を用いることで、フォーム作成が簡単になります。

257  	function form( $instance ) {
258          $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'count' => 0, 'dropdown' => '') );
259          $title = strip_tags($instance['title']);
260          $count = $instance['count'] ? 'checked="checked"' : '';
261          $dropdown = $instance['dropdown'] ? 'checked="checked"' : '';
262  ?>
263          <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>
(省略)

ウィジェット登録

ワードプレスフックに、widgets_init があるので、自分で定義したウィジェットを追加します。 下の例では、hogehoge_widgets という名前にしていますが、任意の名前で OK です。

add_action( 'widgets_init', 'hogehoge_widgets' );

function hogehoge_widgets() {
	register_widget( 'ウィジェットクラス名' );
}

※ この記事は、2009年5月時点の情報をもとに、2.8ベータで検証したものです。 正式リリース時に変更される可能性があります。

Copyright (C) 2008-2011. さくらインターネットでWordPress All rights reserved.