Advertisement
elbeth

Untitled

Oct 24th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.09 KB | None | 0 0
  1. <?php
  2. /**
  3. *Custom Widget for call to action button 105x35px
  4. */
  5.  
  6.  
  7.  add_action( 'widgets_init', 'button_widget' );
  8.  
  9.  
  10. function button_widget() {
  11.     register_widget( 'Button_Widget' );
  12. }
  13.  
  14. class Button_Widget extends WP_Widget {
  15.  
  16. function Button_Widget() {
  17.         $widget_ops = array( 'classname' => 'main-banner-button', 'description' => __('Displays widget for call to action button', 'button') );
  18.        
  19.         $control_ops = array( 'width' => 105, 'height' => 35, 'id_base' => 'button-widget' );
  20.        
  21.         $this->Button_Widget( 'button-widget', __('Button Widget', 'main-banner-button'), $widget_ops, $control_ops );
  22.     }
  23.     // Widget init
  24.   public function __construct() {
  25.      parent::__construct(
  26.         'button_widget',
  27.         'Custom Widget: Button Widget',
  28.         array('description' => __('Button Widget','Displays widget for call to action button'), )
  29.      );
  30.   }
  31.    
  32.  
  33.   // Output the Widget options in the back-end
  34.   public function form($instance) {
  35.     $defaults = array(
  36.         'title' => __('Button Widget'),
  37.         'ad_link' => 'http://wp.tutsplus.com',
  38.         'ad_img' => '/bg-btn.gif'
  39.         );
  40.        
  41.     $instance = wp_parse_args((array) $instance, $defaults);
  42.    
  43. ?>
  44.  
  45. <!-- The Title -->
  46.  
  47.     <p>
  48.         <label for="<?php echo $this->get_field_id('title') ?>"><?php _e('Title'); ?></label>
  49.         <input type="text" id="<?php echo $this->get_field_id('title') ?>" name="<?php echo $this->get_field_name('title') ?>" class="widefat" value="<?php echo esc_attr($instance['title']); ?>" />
  50.     </p>
  51.    
  52.    
  53. <!-- The Ad Link -->
  54.    
  55.     <p>
  56.         <label for="<?php echo $this->get_field_id('ad_link'); ?>"><?php _e('Ad Link'); ?></label>
  57.         <input type="text" id="<?php echo $this->get_field_id('ad_link'); ?>" name="<?php echo $this->get_field_name('ad_link'); ?>" class="widefat" value="<?php echo esc_attr($instance['ad_link']); ?>" />
  58.     </p>
  59.    
  60.    
  61. <!-- The Ad Image -->
  62.    
  63.     <p>
  64.         <label for="<?php echo $this->get_field_id('ad_img') ?>"><?php _e('Ad Image'); ?></label>
  65.         <input type="text" id="<?php echo $this->get_field_id('ad_img') ?>" name="<?php echo $this->get_field_name('ad_img') ?>" class="widefat" value="<?php echo esc_attr($instance['ad_img']); ?>" />
  66.     </p>
  67.    
  68. <?php
  69.    
  70.   }
  71.   // Process widget options for saving
  72.   public function update($new_instance, $old_instance) {
  73.     $instance = $old_instance;
  74.    
  75.     // Title
  76.     $instance['title'] = strip_tags($new_instance['title']);
  77.    
  78.     // The Link
  79.     $instance['ad_link'] = $new_instance['ad_link'];
  80.    
  81.     // The Image
  82.     $instance['ad_img'] = $new_instance['ad_img'];
  83.    
  84.     return $instance;
  85.    
  86.   }
  87.   //Displays the widget on the page
  88.   public function widget($args, $instance) {
  89.     extract($args);
  90.    
  91.     $title = apply_filters('widget-title', $instance['title']);
  92.    
  93.     $ad_img = $instance['ad_img'];
  94.     $ad_link = $instance['ad_link'];
  95.    
  96.     echo $before_widget;
  97.    
  98.     if (isset($title)) {
  99.         echo $before_title . $title . $after_title;    
  100.     }
  101.  
  102.     $text_link = '<a class="button">';
  103.    
  104.     if ($ad_img) {
  105.    
  106.     $text_link .= '<a class="button" href="<?php echo $ad_link; ?>"> Read More </a>';
  107.    
  108.     }
  109.    
  110.     $text_link .= '</a>';
  111.    
  112.     echo $text_link;
  113.    
  114.    
  115.     echo $after_widget;
  116.   }
  117. }
  118. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement