Created
December 12, 2012 02:35
-
-
Save aprakasa/4264378 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Caching Widgets with transients | |
* | |
* @link http://speckyboy.com/2011/12/14/website-speed-part-3-caching-wordpress/ | |
* / | |
class show_ak_events_Widget extends WP_Widget { | |
function show_ak_events_Widget() { | |
/* Widget settings. */ | |
$widget_ops = array( 'classname' => 'ak-events', 'description' => 'Shows events in a table' ); | |
/* Widget control settings. */ | |
$control_ops = array( 'width' => 300, 'height' => 350, 'id_base' => 'ak-events' ); | |
/* Create the widget. */ | |
$this->WP_Widget( 'ak-events', 'Show Events', $widget_ops, $control_ops ); | |
} | |
function widget( $args, $instance ) { | |
extract( $args ); | |
// get cache if it exists | |
// $widget_id comes from the widget $args->widget_id and is the widgets unique ID | |
$output = get_transient('events'.$widget_id); | |
// if no $output do stuff inside this if statement | |
if ( $output === false ) { | |
// set the title variable | |
$title = apply_filters('widget_title', $instance['title'] ); | |
// standard opening of widget | |
$output = $before_widget; | |
// if a title exists add it to the top of the widget | |
$output .= ( !empty( $title ) )? $before_title . $title . $after_title : "" ; | |
// Create query arguments for WP_Query to use | |
$widgetargs = array( 'posts_per_page'=>'-1', | |
'post_type'=>'ak_events', | |
'post_status'=>'publish' | |
); | |
// WP_Query sets up a loop query | |
$query = new WP_Query( $widgetargs ); | |
// create the opening table and top row | |
$output .= "<table><tr><th>Event Name</th><th>Information</th></tr>"; | |
// If the WP_Query has results send them through the loop | |
if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); | |
$output .= "<tr><td>" . get_the_title() . "</td><td> " . get_the_excerpt() . " </td></tr>"; | |
endwhile;endif; | |
// close the table | |
$output .= "</table>"; | |
// close widget properly | |
$output .= $after_widget; | |
// save $output as a transient and set it to be 60 seconds * 5 = 5 minutes. | |
// | |
set_transient( 'events'.$widget_id, $output, 60*5 ); | |
} | |
echo $output; | |
} | |
function update( $new_instance, $old_instance ) { | |
// save form data | |
$instance = $old_instance; | |
$instance['title'] = $new_instance['title']; | |
// delete the transient so the new title setting is used | |
delete_transient('events'.$this->id); | |
return $instance; | |
} | |
function form( $instance ) { | |
$defaults = array( | |
'title'=>'' | |
); | |
$instance = wp_parse_args( (array) $instance, $defaults ); ?> | |
<p> | |
<label for="<?php echo $this->get_field_id( 'title' ); ?>"> | |
<?php _e('Title:','proving-ground'); ?> | |
</label> | |
<input id="<?php echo $this->get_field_id( 'title' ); ?>" | |
name="<?php echo $this->get_field_name( 'title' ); ?>" | |
value="<?php echo $instance['title']; ?>" style="width:95%" /> | |
</p> | |
<?php | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment