Advertisement
afsarwebdev

wp custom widget

May 6th, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. // get sidebar
  2. <div class="cw_single">
  3. <?php get_sidebar(); ?>
  4. </div>
  5.  
  6. //Custom widget
  7. <?php
  8.  
  9. class Adaptive_Ad_260_Widget extends WP_Widget {
  10. // widget init
  11. public function __construct() {
  12. parent::__construct(
  13. 'adaptice_ad_w',
  14. 'Custom Widget: 260x120 Ad',
  15. array('description' => __('Display a single 260x120 ad block', 'custom_widget')) //custom_widget text domain
  16. );
  17. }
  18. // output the widget options in the back-end
  19. public function form($instance) {
  20. $defaults = array(
  21. 'title' => __('Ad 260x120', 'custom_widget'),
  22. 'ad_img' => IMAGES . '/quote-format-icon.png',
  23. 'ad_link' => 'http://wp.tutsplus.com',
  24. 'dsc' => __('Description', 'custom_widget'),
  25. );
  26.  
  27. $instance = wp_parse_args((array) $instance, $defaults);
  28.  
  29. ?>
  30. <!-- title -->
  31. <p>
  32. <label for="<?php echo $this->get_field_id('title') ?>"><?php _e('Title', 'custom_widget'); ?></label>
  33. <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']); ?>">
  34. </p>
  35.  
  36. <!-- ad image -->
  37. <p>
  38. <label for="<?php echo $this->get_field_id('ad_img') ?>"><?php _e('Ad Image', 'custom_widget'); ?></label>
  39. <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']); ?>">
  40. </p>
  41. <!-- ad link -->
  42.  
  43. <p>
  44. <label for="<?php echo $this->get_field_id('ad_link') ?>"><?php _e('Ad Link', 'custom_widget'); ?></label>
  45. <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']); ?>">
  46. </p>
  47.  
  48.  
  49.  
  50. <?php
  51. }
  52.  
  53. // Process widget options for saving
  54. public function update($new_instance, $old_instance) {
  55. $instance = $old_instance;
  56.  
  57. // Title
  58. $instance['title'] = strip_tags($new_instance['title']);
  59.  
  60. // Ad image
  61. $instance['ad_img'] = strip_tags($new_instance['ad_img']);
  62.  
  63. // Ad link
  64. $instance['ad_link'] = strip_tags($new_instance['ad_link']);
  65.  
  66. return $instance;
  67. }
  68.  
  69. // Display the widget on the page
  70. public function widget($args, $instance) {
  71. extract($args);
  72.  
  73. $title = apply_filters('widget-title', $instance['title']);
  74. $ad_img = $instance['ad_img'];
  75. $ad_link = $instance['ad_link'];
  76.  
  77. if ($title) {
  78. echo $before_title . $title . $after_title;
  79. }
  80.  
  81. echo '<div class="cw_single">';
  82. if($ad_img) : ?>
  83. <a href="<?php echo $ad_link; ?>"><img src="<?php echo $ad_img ?>" alt=""></a>
  84.  
  85. <?php endif;
  86. echo '</div>';
  87. echo $after_widget;
  88. }
  89. }
  90. register_widget('Adaptive_Ad_260_Widget');
  91.  
  92. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement