質問: array( 'classname' => 'widget_avatar', 'description' => __( "my avatar widget" ) ); $this->wp_widget('my_avatar', __('my avatar'), $widget_ops); } } add_action('widgets_init', create_function('', "register_widget('my_widget_avatar');"));
回答: 完全なコードclass my_widget_avatar extends wp_widget { function my_widget_avatar() { $widget_ops = array( 'classname' => 'widget_avatar', 'description' => __( "my avatar widget" ) ); $this->wp_widget('my_avatar', __('my avatar'), $widget_ops); } function widget( $args, $instance ) { extract($args); $text = apply_filters( 'widget_text', $instance['text'], $instance ); echo $before_widget; ?> <div class="textwidget"> <div class="avatar"> <div class="avatartext"> <p><?php echo $text; ?></p> </div> </div> <div class="avatarimage"></div> </div> <?php echo $after_widget; } function update( $new_instance, $old_instance ) { $instance = $old_instance; if ( current_user_can('unfiltered_html') ) $instance['text'] = $new_instance['text']; else $instance['text'] = stripslashes( wp_filter_post_kses( addslashes($new_instance['text']) ) ); // wp_filter_post_kses() expects slashed return $instance; } function form( $instance ) { $instance = wp_parse_args( (array) $instance, array( 'text' => '' ) ); $text = format_to_edit($instance['text']); ?> <textarea class="widefat" rows="16" cols="20" id="<?php echo $this->get_field_id('text'); ?>" name="<?php echo $this->get_field_name('text'); ?>"><?php echo $text; ?></textarea> <?php } } function lbi_widgets_init() { register_widget( 'my_widget_avatar' ); } add_action( 'widgets_init', 'lbi_widgets_init' );