Skip to content

Instantly share code, notes, and snippets.

@zinntikumugai
Created September 19, 2016 09:25
Show Gist options
  • Save zinntikumugai/bf9cfef7fda5bf4b5210881c07cc2b80 to your computer and use it in GitHub Desktop.
Save zinntikumugai/bf9cfef7fda5bf4b5210881c07cc2b80 to your computer and use it in GitHub Desktop.
//////////////////////////////////////
//Zinntikumugai_Push7_Widget
//another: zinntikumugai
//ver 1.0
//////////////////////////////////////
class Zinntikumugai_Push7_Widget extends WP_Widget {
function __construct() {
$widget_ops = array (
'classname' => 'zinntikumugai_push7_widget',
'description' => 'Push7を埋め込めるウィジェット',
'customize_selective_refresh' => true,
);
parent::__construct( false, $name = '人畜無害のPush7ウィジェット', $widget_ops );
}
/**
* ウィジェットを出力
* @param array $args
* @param array $instance 設定項目
*/
public function widget( $args, $instance ) {
/*
$viewtype = $instance['viewtype'];
$id = $instance['id'];
$jsuri = $instance['jsuri'];
*/
$title = apply_filters( 'widget_title', empty($instance['title']) ? 'Push7' : $instance['title'] , $instance, $this->id_base );
$id = empty($instance['p7id']) ? '000000' : $instance['p7id'];
$viewtype = empty($instance['p7c']) ? 'n' : $instance['p7c'];
$jsuri = empty($instance['p7src']) ? 'https://push7.jp/' : $instance['p7src'];
echo $args['before_widget'];
if( $title ) {
echo $args['before_title'] . $title . $args['after_title'];
}
echo "<div class=\"p7-b\" data-p7id=\"{$id}\" data-p7c=\"{$viewtype}\"></div>";
echo "<script src=\"{$jsuri}\" charset=\"UTF-8\"></script>";
echo $args['after_widget'];
}
/**
* ウィジェット管理画面を出力
* @param array $instance 設定項目
*/
public function form( $instance ) {
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = sanitize_text_field( $instance['title'] );
$p7id = sanitize_text_field( $instance['p7id'] );
$p7c = sanitize_text_field( $instance['p7c'] );
$p7src = sanitize_text_field( $instance['p7src'] );
?>
<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); ?>" />
<label for="<?php echo $this->get_field_id('p7id'); ?>"><?php _e('APPNO:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('p7id'); ?>" name="<?php echo $this->get_field_name('p7id'); ?>" type="text" value="<?php echo esc_attr($p7id); ?>" />
<label for="<?php echo $this->get_field_id('p7c'); ?>"><?php _e('表示形式:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('p7c'); ?>" name="<?php echo $this->get_field_name('p7c'); ?>" type="text" value="<?php echo esc_attr($p7c); ?>"/>
<label for="<?php echo $this->get_field_id('p7c'); ?>"><?php _e('scriptURI:'); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id('p7src'); ?>" name="<?php echo $this->get_field_name('p7src'); ?>" type="text" value="<?php echo esc_attr($p7src); ?>" />
</p>
<?php
}
/**
* データ更新
*/
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['p7id'] = sanitize_text_field( $new_instance['p7id'] );
$instance['p7c'] = sanitize_text_field( $new_instance['p7c'] );
$instance['p7src'] = sanitize_text_field( $new_instance['p7src'] );
return $instance;
}
}
add_action('widgets_init', create_function('', 'return register_widget("Zinntikumugai_Push7_Widget");'));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment