Skip to content

Instantly share code, notes, and snippets.

@manmar
Created June 23, 2015 08:53
Show Gist options
  • Save manmar/e62188cb5c7acda5610c to your computer and use it in GitHub Desktop.
Save manmar/e62188cb5c7acda5610c to your computer and use it in GitHub Desktop.
Image module WP widget
<?php
class Image_Module_Widget extends WP_Widget {
function __construct() {
parent::__construct(
'image_module_widget', // Base ID
__( 'Image module', 'text-domain' ), // Name
array( 'description' => __( 'Image module - full width image', 'text-domain' ), ) // Args
);
add_action('admin_enqueue_scripts', array($this, 'upload_scripts'));
}
// upload scripts for enabling media uploader
public function upload_scripts() {
wp_enqueue_script('media-upload');
wp_enqueue_script('thickbox');
wp_enqueue_script('upload_media_widget', CHILD_URL . '/lib/upload_media.js', array('jquery'));
wp_enqueue_style('thickbox');
}
function form( $instance ) {
$image_module = $instance[ 'image_module' ];
$image_module_src = $instance[ 'image_module_src' ];
// markup for form ?>
<div class="widget_image">
<!-- get the id -->
<input class="meta_box_upload_image" type="hidden" id="<?php echo $this->get_field_id( 'image_module' ); ?>" name="<?php echo $this->get_field_name( 'image_module' ); ?>" value="<?php echo esc_attr( $image_module ); ?>">
<!-- get src -->
<input class="meta_box_upload_image_src" type="hidden" id="<?php echo $this->get_field_id( 'image_module_src' ); ?>" name="<?php echo $this->get_field_name( 'image_module_src' ); ?>" value="<?php echo esc_attr( $image_module_src ); ?>">
<!-- preview image -->
<?php if ($image_module_src): ?>
<?php $image_medium = wp_get_attachment_image_src( $image_module, 'medium'); ?>
<img class="meta_box_upload_image_preview" src="<?php echo $image_medium[0]; ?>" alt="">
<?php else: ?>
<img class="meta_box_upload_image_preview" src="<?php echo CHILD_URL . '/assets/img/no-image.png' ?> " alt="image"><br>
<?php endif ?>
<br>
<button class="upload_image_button" style="margin: 10px 0 15px; background: #000; color: #fff; border: none; text-transform: uppercase; font-size: 12px; padding: 8px 12px;">Add image</button>
</div>
<?php } // form
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance[ 'image_module' ] = strip_tags( $new_instance[ 'image_module' ] );
return $instance;
} // update
function widget( $args, $instance ) {
echo $args['before_widget'];
$the_image = $instance['image_module'];
// check if $the_image has values
if( $the_image ) { ?>
<section class="image-section text-center">
<div class="container-fluid">
<div class="row">
<div class="col-xs-12">
<?php $img = wp_get_attachment_image_src( $the_image, 'full')?>
<img src="<?php echo $img[0]; ?>" alt="digital">
</div>
</div>
</div>
</section><!-- /.image-section -->
<?php }
} // widget
} // class
function register_image_module_widget() {
register_widget( 'Image_Module_Widget' );
}
add_action( 'widgets_init', 'register_image_module_widget' );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment