wordpressのプラグインでウィジェット作成をためす。

プラグインウィジェットを作る。

$ pwd
/path/to/wordpressproject/wp-content/plugins
$ tree helloworld/
helloworld/
`-- helloworld.php

0 directories, 1 file

helloworld.phpの中身。

<?php
/*
Plugin Name: HelloWorld
Plugin URI: http://d.hatena.ne.jp/mustankatti/
Description: description of hello world.
Author: mustankatti
Version: 0.0
Author URI: http://d.hatena.ne.jp/mustankatti/
Description: no description
 */

class HelloWorld_Widget extends WP_Widget {
        public function __construct() {
                parent::__construct(
                        'helloworld_widget', // Base ID
                        'HelloWorld_Widget', // Name
                        array( 'description' => __('A Hello World Widget', 'text_domain'), )
                );
        }

        public function widget( $args, $instance) {
                extract($args);
                $title = apply_filters('widget_title', $instance['title']);
                echo $before_widget;
                if (!empty($title))
                        echo $before_title . $title . $after_title;
                ?>Hello, World!<?php
                echo $after_widget;
        }

        public function update( $new_instance, $old_instance ) {
                $instance = $old_instance;
                $instance['title'] = strip_tags($new_instance['title']);
                return $instance;
        }
        public function form( $instance ) {
                if ( $instance ) {
                        $title = esc_attr($instance['title']);
                }
                else {
                        $title = __('New title', 'text_domain');
                }
                ?>
                <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>
                <?php
        }
}

add_action('widgets_init',
        create_function('', 'register_widget("helloworld_widget");')
);

?>

プラグインより有効化。
外観のwidgetからサイドバーに追加。