Lego2012
12/13/2016 - 3:47 PM

Custom Widget

Custom Widget

<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

<!-- In Core Functionality plugin's plugin.php: -->

include_once( BE_DIR . '/lib/widgets/widget-upcoming-events.php' );
<?php
//* Do NOT include the opening php tag shown above. Copy the code shown below.

/**
 * Adds SK_Upcoming_Events_Widget widget.
 */
class SK_Upcoming_Events_Widget extends WP_Widget {

  /**
   * Register widget with WordPress.
   */
  function __construct() {
    parent::__construct(
      'upcoming_events', // Base ID
      __( 'Upcoming Events', 'text_domain' ), // Name
      array( 'description' => __( 'Displays the 6 latest Events', 'text_domain' ), ) // Args
    );
  }

  /**
   * Front-end display of widget.
   *
   * @see WP_Widget::widget()
   *
   * @param array $args     Widget arguments.
   * @param array $instance Saved values from database.
   */
  public function widget( $args, $instance ) {

    // WP_Query arguments
    $args_upcoming_events = array (
      'post_type'              => 'e4gf_events',
      'posts_per_page'         => '6',
    );

    // The Query
    $upcoming_events_query = new WP_Query( $args_upcoming_events );

    // The Loop
    if ( $upcoming_events_query->have_posts() ) {
      echo $args['before_widget'];
      if ( ! empty( $instance['title'] ) ) {
        echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
      }
      while ( $upcoming_events_query->have_posts() ) {
        $upcoming_events_query->the_post(); ?>
        <article <?php post_class(); ?>>
          <?php

            if ( has_post_thumbnail() ) {
              $image = genesis_get_image( 'format=url&size=home-bottom' );
              printf( '<div class="upcoming-events-image"><a href="%s" rel="bookmark"><img src="%s" alt="%s" class="alignleft" /></a></div>', get_permalink(), $image, the_title_attribute( 'echo=0' ) );
            }
          ?>

          <div class="right">
            <!-- Display the Title as a link to the Post's permalink. -->
            <h2 class="entry-title"><a href="<?php the_permalink(); ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>
            <?php $event_date = get_post_meta( get_the_ID(), '_e4gf_event_meta_date', true );
            echo '<div class="entry-date">' . date( 'j F Y', strtotime( $event_date ) ) . '</div>'; ?>

            <a href="<?php the_permalink(); ?>" rel="bookmark" title="View Event: <?php the_title_attribute(); ?>" class="view-event-link">View Event +</a>
          </div>
          
        </article>
      <?php }
      echo $args['after_widget'];
    } else {
      // no posts found
    }

    // Restore original Post Data
    wp_reset_postdata();
    
  }

  /**
   * Back-end widget form.
   *
   * @see WP_Widget::form()
   *
   * @param array $instance Previously saved values from database.
   */
  public function form( $instance ) {
    $title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'Upcoming Events', 'text_domain' );
    ?>
    <p>
    <label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label> 
    <input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
    </p>
    <?php 
  }

  /**
   * Sanitize widget form values as they are saved.
   *
   * @see WP_Widget::update()
   *
   * @param array $new_instance Values just sent to be saved.
   * @param array $old_instance Previously saved values from database.
   *
   * @return array Updated safe values to be saved.
   */
  public function update( $new_instance, $old_instance ) {
    $instance = array();
    $instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';

    return $instance;
  }

} // class SK_Upcoming_Events_Widget

// register SK_Upcoming_Events_Widget widget
function upcoming_events() {
  register_widget( 'SK_Upcoming_Events_Widget' );
}
add_action( 'widgets_init', 'upcoming_events' );