Skip to content

Instantly share code, notes, and snippets.

@Shitsu
Last active April 11, 2017 02:39
Show Gist options
  • Save Shitsu/292a47ed1854be2b9e5e6197a45a1258 to your computer and use it in GitHub Desktop.
Save Shitsu/292a47ed1854be2b9e5e6197a45a1258 to your computer and use it in GitHub Desktop.
[WordPress]ウィジェット作成用のテンプレート
<?php
/**
* Class Widget_Template
*/
class Widget_Template extends WP_Widget {
/**
* Widget_Template コンストラクタ
*/
public function __construct() {
$widget_options = array(
'classname' => 'widget-template',
'description' => 'テンプレートウィジェットの説明文です。',
'customize_selective_refresh' => true,
);
$control_options = array( 'width' => 400, 'height' => 350 );
parent::__construct( 'widget-template', 'テンプレートウィジェット', $widget_options, $control_options );
}
/**
* ウィジェットの内容をWebページに出力します(HTML表示)
*
* @param array $args register_sidebar()で設定したウィジェットの開始/終了タグ、タイトルの開始/終了タグなどが渡される。
* @param array $instance 管理画面から入力した値が渡される。
*/
public function widget( $args, $instance ) {
/** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
$widget_text = ! empty( $instance['text'] ) ? $instance['text'] : '';
echo $args['before_widget'];
if ( ! empty( $title ) ) {
echo $args['before_title'] . $title . $args['after_title'];
} ?>
<div class="textwidget"><?php echo $widget_text; ?></div>
<?php
echo $args['after_widget'];
}
/**
* 管理画面のウィジェット設定フォームを出力します。
*
* @param array $instance 現在のオプション値が渡される。
*/
public function form( $instance ) {
$defaults = array(
'title' => '',
'text' => ''
);
$instance = wp_parse_args( (array) $instance, $defaults );
$title = sanitize_text_field( $instance['title'] );
?>
<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( 'text' ); ?>"><?php _e( 'Content:' ); ?></label>
<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 esc_textarea( $instance['text'] ); ?></textarea>
</p>
<?php
}
/**
* ウィジェットオプションのデータ検証/無害化
*
* @param array $new_instance 新しいオプション値
* @param array $old_instance 以前のオプション値
*
* @return array データ検証/無害化した値を返す
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['text'] = $new_instance['text'];
} else {
$instance['text'] = wp_kses_post( $new_instance['text'] );
}
return $instance;
}
}
/**
* ウィジェットテンプレートの登録
*/
function theme_register_widget() {
register_widget( 'Widget_Template' );
}
add_action( 'widgets_init', 'theme_register_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment