Skip to content

Instantly share code, notes, and snippets.

@pimpmywp
Last active December 10, 2015 05:38
Show Gist options
  • Save pimpmywp/4388521 to your computer and use it in GitHub Desktop.
Save pimpmywp/4388521 to your computer and use it in GitHub Desktop.
PM_Uploader を利用したバナーウィジェット(WordPress 3.5以降)
<?php
if ( ! class_exists( 'PM_Widget_Banner' ) ) {
add_action( 'widgets_init', create_function( '', 'new PM_Uploader; register_widget("PM_Widget_Banner");' ) );
require_once( 'class-pm-uploader.php' );
class PM_Widget_Banner extends WP_Widget {
function __construct() {
$widget_ops = array( 'description' => 'バナーを表示します' );
parent::__construct( 'banner', 'バナー', $widget_ops );
}
function widget( $args, $instance ) {
$blank = isset( $instance['blank'] ) ? $instance['blank'] : 0;
$url = isset( $instance['url' ] ) ? $instance['url' ] : '';
$alt = isset( $instance['alt' ] ) ? $instance['alt' ] : '';
$image = isset( $instance['image'] )
? wp_get_attachment_image( intval( $instance['image'] ), 'full', false, array( 'alt' => $alt ) )
: '';
if ( 0 !== strpos( $url, 'http') )
$url = trailingslashit( home_url( $url ) );
printf(
'<a href="%1$s"%2$s>%3$s</a>'
, esc_url( $url )
, ( $blank ? ' target="_blank"' : '' )
, $image
);
}
function update( $new_instance, $old_instance ) {
$instance['blank'] = intval( $new_instance['blank'] );
$instance['image'] = intval( $new_instance['image'] );
$instance['alt' ] = strip_tags( $new_instance['alt'] );
$instance['url' ] = $new_instance['url'];
return $instance;
}
function form( $instance ) {
$blank = isset( $instance['blank'] ) ? intval ( $instance['blank'] ) : 0;
$image = isset( $instance['image'] ) ? intval ( $instance['image'] ) : 0;
$alt = isset( $instance['alt' ] ) ? strip_tags( $instance['alt' ] ) : '';
$url = isset( $instance['url' ] ) ? esc_attr ( $instance['url' ] ) : '';
?>
<div>
<label for="<?php echo $this->get_field_id('image'); ?>">Image:</label>
<?php PM_Uploader :: render( array( 'media_id' => $image, 'field_id' => 'image' ), $this ) ?>
</div>
<p>
<label for="<?php echo $this->get_field_id('alt'); ?>">Alt:</label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('alt'); ?>" name="<?php echo $this->get_field_name('alt'); ?>" value="<?php echo $alt; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('url'); ?>">URL:</label>
<input type="text" class="widefat" id="<?php echo $this->get_field_id('url'); ?>" name="<?php echo $this->get_field_name('url'); ?>" value="<?php echo $url; ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id('blank'); ?>">
<input type="checkbox" id="<?php echo $this->get_field_id('blank'); ?>" name="<?php echo $this->get_field_name('blank'); ?>" value="1" <?php checked($blank) ?>/> Open New Window</label>
</p>
<?php
}
}
}
<?php
/*
Plugin Name: Pimp My Banner Widget
Plugin URI: http://pimpmysite.net/archives/137
Description: Banner widget.
Author: Pimp My WP
Author URI: http://pimpmysite.net
Version: 1.0
*/
require_once( 'class-pm-widget-banner.php' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment