Skip to content

Instantly share code, notes, and snippets.

@WordPress-Handbuch
Last active April 11, 2019 07:18
Show Gist options
  • Save WordPress-Handbuch/e2c627bcafe32a3b42912433e5dd8c3d to your computer and use it in GitHub Desktop.
Save WordPress-Handbuch/e2c627bcafe32a3b42912433e5dd8c3d to your computer and use it in GitHub Desktop.
Example WordPress 5 widget to display a configurable number of event entries from the database, originally populated through a new custom post type
<?php
/**
* @package WH_Event_Widget
* @version 1.0.0
*/
/*
Plugin Name: WH Event Widget
Plugin URI: https://wordpress-handbuch.com
Description: Anzeige aktueller Veranstaltungen
Author: Johannes Mustermann
Version: 1.0.0
Author URI: https://ihredomain
*/
class WH_Event_Widget extends WP_Widget {
public function __construct() {
$widget_options = array(
'classname' => 'wh_event_widget',
'description' => 'Anzeige aktueller Veranstaltungen',
);
parent::__construct( 'wh_event_widget', 'Aktuelle Veranstaltungen', $widget_options );
}
/* Section 2: */
public function form( $instance ) {
$title = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
$number = isset( $instance['number'] ) ? absint( $instance['number'] ) : 3; ?>
<p>
<label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?> <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); ?>" /></label>
</p>
<p>
<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Anzahl der Veranstaltungen ab heute:' ); ?></label>
<input class="tiny-text" id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>" type="number" step="1" min="1" value="<?php echo $number; ?>" size="3" />
</p><?php
}
/* Section 3: */
public function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = sanitize_text_field( $new_instance['title'] );
$instance['number'] = (int) $new_instance['number'];
return $instance;
}
/* Section 4: */
public function widget( $args, $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : 'Veranstaltungen';
$number = ( ! empty( $instance['number'] ) ) ? absint( $instance['number'] ) : 3;
$result = new WP_Query( array(
'post_type' => 'wh_event',
'posts_per_page' => $number,
'no_found_rows' => true,
'post_status' => 'publish',
'meta_key' => 'eventdate',
'meta_compare' => '>=',
'meta_value' => date('c',time()),
'orderby' => 'meta_value',
'order' => 'ASC',
), $instance );
if ( ! $result->have_posts() ) {
return;
}
/* Section 5: */
echo $args['before_widget'];
if ( $title ) { echo $args['before_title'] . $title . $args['after_title']; }
?>
<ul>
<?php foreach ( $result->posts as $event ) : ?>
<?php
$post_title = get_the_title( $event->ID );
$title = ( ! empty( $post_title ) ) ? $post_title : __( '(no title)' );
$custom = get_post_custom( $event->ID );
$location = ( isset($custom['location'][0] )) ? 'in ' . $custom['location'][0] : '';
$eventdate = ( isset($custom['eventdate'][0] )) ? 'am ' . date_i18n(get_option( 'date_format' ), strtotime($custom['eventdate'][0])) : '';
?>
<li>
<a href="<?php the_permalink( $event->ID ); ?>"><strong><?php echo $title ; ?></strong></a><br/><?php echo $eventdate; ?> <?php echo $location; ?>
</li>
<?php endforeach; ?>
</ul>
<?php
echo $args['after_widget'];
}
}
/* Section 6: */
function register_wh_event_widget() {
register_widget( 'wh_event_widget' );
}
add_action( 'widgets_init', 'register_wh_event_widget' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment