Advertisement
Grork

Create WordPress widget

Mar 3rd, 2016
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.62 KB | None | 0 0
  1. <?php
  2. // Creazione Widget
  3. class banner1 extends WP_Widget {
  4.     function __construct() {
  5.         parent::WP_Widget('Banner1', 'Banner #1', array('description' => 'Primo banner'));
  6.     }
  7.  
  8.     function widget($args, $instance) {
  9.         //estraggo variabili da args:
  10.         extract($args);
  11.         $str = "<div id='sponsor1' class='sponsor'><a href='";
  12.         $str .= $instance['link'];
  13.         $str .= "'>";
  14.         if(!empty($instance['img'])) {
  15.             $str .= "<img width='230' height='130' src='";
  16.             $str .= get_site_url();
  17.             $str .= "/wp-content/themes/atletico_luinese/images/";
  18.             $str .= $instance['img'];
  19.             $str .= "' />";
  20.         }
  21.         $str .= "</a></div>";
  22.         echo $str;
  23.     }
  24.  
  25.     function update($new_instance, $old_instance) {
  26.         $instance = $old_instance;
  27.         $instance['img'] = strip_tags($new_instance['img']);
  28.         $instance['link'] = strip_tags($new_instance['link']);
  29.         return $instance;
  30.     }
  31.  
  32.     function form($instance) {
  33.     if ( $instance ) {
  34.         //codifico html
  35.         $img = esc_attr($instance['img']);
  36.         $link = esc_attr($instance['link']);
  37.     }
  38.     // Campi per modificare
  39.     ?>
  40.     <label for="<?php echo $this -> get_field_id('link');?>"><?php _e('Link:'); ?></label>
  41.     <input id="<?php echo $this -> get_field_id('link');?>" class="widefat" type="text" name="<?php echo $this -> get_field_name('link');?>" value="<?php echo $link; ?>"
  42.     <label for="<?php echo $this -> get_field_id('img');?>"><?php _e('Immagine:'); ?></label>
  43.     <input id="<?php echo $this -> get_field_id('img');?>" class="widefat" type="text" name="<?php echo $this -> get_field_name('img');?>" value="<?php echo $img;?>" />
  44.     <?php }
  45. }
  46.  
  47. add_action('widgets_init', create_function('', 'register_widget("banner1");'));
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement