Advertisement
Beee

event tags widget

May 31st, 2017
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.53 KB | None | 0 0
  1. <?php
  2.     class EventTags extends WP_Widget {
  3.  
  4.         public function __construct() {
  5.  
  6.             // Instantiate the parent object
  7.  
  8.             parent::__construct(
  9.                 'event-tags',     // Base ID
  10.                 'Event tags',      // Name
  11.                 array(
  12.                     'classname'   => 'widget--event tags',
  13.                     'description' => 'Shows event tags'
  14.                 ) // Args
  15.             );
  16.         }
  17.  
  18.         public function widget( $args, $instance ) {
  19.  
  20.             $term_args = array(
  21.                 'taxonomy' => 'event-tags',
  22.                 'orderby'  => 'count',
  23.                 'order'    => 'DESC',
  24.                 'number'   => 10,
  25.             );
  26.             $terms = get_terms( $term_args );
  27.  
  28.             // Widget output
  29.             extract( $args );
  30.             $etw_widgettitle = apply_filters( 'widget_title', $instance['etw_widgettitle'] );
  31.  
  32.             echo $before_widget;
  33.  
  34.             if ( $etw_widgettitle ) {
  35.                 echo $before_title . $etw_widgettitle . $after_title;
  36.             }
  37.  
  38.             if ( $terms ) {
  39.  
  40.                 echo '<ul>';
  41.                 foreach( $terms as $term ) {
  42.                     echo '<li><a href="' . get_term_link( $term->term_taxonomy_id ) . '">' . $term->name . '</a></li>';
  43.                 }
  44.                 echo '</ul>';
  45.             }
  46.  
  47.             echo $after_widget;
  48.         }
  49.  
  50.         public function update( $new_instance, $old_instance ) {
  51.             // Save widget options
  52.             $instance = $old_instance;
  53.             $instance['etw_widgettitle'] = strip_tags($new_instance['etw_widgettitle']);
  54.             return $instance;
  55.         }
  56.  
  57.         public function form( $instance ) {
  58.             // Output admin widget options form
  59.             $etw_widgettitle = ! empty( $instance['etw_widgettitle'] ) ? $instance['etw_widgettitle'] : esc_html__( 'Event tags', 'text_domain' );
  60.             ?>
  61.             <p>
  62.                 <label for="<?php echo $this->get_field_id('etw_widgettitle'); ?>">Widget title:</label>
  63.                 <input class="widefat" id="<?php echo $this->get_field_id('etw_widgettitle'); ?>" name="<?php echo $this->get_field_name('etw_widgettitle'); ?>" type="text" value="<?php if ( $etw_widgettitle ) { echo $etw_widgettitle; } ?>" />
  64.             </p>
  65.         <?php }
  66.     }
  67.  
  68.     function register_my_widgets() {
  69.         if ( function_exists( 'em_content' ) ) {
  70.             register_widget( 'EventTags' );
  71.         }
  72.     }
  73.     add_action( 'widgets_init', 'register_my_widgets' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement