teamadesign

almost production version

Feb 25th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1. <?php
  2. function bg_image_plugin_loaded() {
  3.         if(class_exists('Simple_Image_Widget')){
  4.                 add_action( 'widgets_init', 'bg_image_widgets_init' );
  5.         } else {
  6.                 add_action('admin_notices', 'bg_image_notices');
  7.         }
  8. }
  9. add_action('plugins_loaded', 'bg_image_plugin_loaded');
  10.  
  11. function bg_image_widgets_init() {
  12.         class BG_Image_Widget extends Simple_Image_Widget {
  13.  
  14.                 function __construct() {
  15.                         $widget_options = array( 'classname' => 'widget_bg_image', 'description' => 'A widget to add images as decorative background patterns.' );
  16.                         parent::__construct( 'bg-image', 'BG Image Widget', $widget_options );
  17.  
  18.                         add_action('simple_image_widget_field-height', array($this, 'bg_field_height'), 10, 2);
  19.                         add_filter('simple_image_widget_instance', array($this, 'bg_update'), 10, 4);
  20.             //add_filter('simple_image_widget_output', 'bg_image_widget_output');
  21.            
  22.                 }
  23.  
  24.                 function form_fields(){
  25.                         return array('height');
  26.                 }
  27.  
  28.                 function bg_field_height($instance) {
  29.                         ?>
  30.                         <p>
  31.                                 <label for="<?php echo $this->get_field_id( 'height' ); ?>"><?php _e( 'Height:', 'bg-image-widget' ); ?></label>
  32.                 <input type="text" name="<?php echo $this->get_field_name( 'height' ); ?>" id="<?php echo $this->get_field_id( 'height' ); ?>" value="<?php echo esc_attr( $instance['height'] ); ?>" class="widefat">
  33.                         </p>
  34.                         <?php
  35.                 }
  36.                
  37.                 function bg_update($instance, $new_instance, $old_instance, $id_base) {
  38.                         // @todo Sanitize custom values.
  39.                         return $instance;
  40.                 }
  41.        
  42.         function render($args, $instance){
  43.                        
  44.             $src = wp_get_attachment_image_src( $instance['image_id'], 'full');
  45.            
  46.             if ( ! empty( $instance['image_id'] ) ) {
  47.                 $output = '<div class="pattern-widget" id="bg-'.urlencode($instance['title']).'" style="background-image:url('.$src[0].'); height:'.$instance['height'].'px;">';
  48.                 $output .= '</div><!---->';
  49.             }else{
  50.                 $output = '<div class="hidden error"><p>There is no image for this, please pick an image first!</p></div>';
  51.             }
  52.             return $output;
  53.         }
  54.  
  55.         }
  56.  
  57.         // Register the widget.
  58.         register_widget( 'BG_Image_Widget' );
  59. }
  60.  
  61. function bg_image_notices(){
  62.         global $pagenow;
  63.         if($pagenow == 'widgets.php'){
  64.                 echo '<div class="updated">No Simple Image Widget Class for BG Image Widget to Use</div>';
  65.         }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment