"Sidebar Widgets" and drag and drop the widget wherever you want to show it.
Thanks
Thanks to Borja Fernandez, Chris Goringe and Sven Weidauer for writing their plugins and to Luis Pérez and Mike Koepke for their contributions.
*/
function widget_atd_init() {
if (!function_exists('register_sidebar_widget') || !function_exists('register_widget_control'))
return;
register_sidebar_widget( 'Around this date', 'widget_atd_display' );
register_widget_control( 'Around this date', 'widget_atd_control', 475, 570 );
}
add_action( 'plugins_loaded', 'widget_atd_init' );
function around_this_date( $daysbefore, $daysafter, $mode, $yearsago, $lastxyears, $sinceyear, $limit, $none, $showyear, $showdate, $dateformat, $showexcerpt) {
$outputlist = ''; // empty the 'outputlist' string
$outputlist .= '
';
switch ($mode) {
case 1: // "classic" mode
$start_ago = (365*$yearsago)+$daysbefore;
$end_ago = (365*$yearsago)-$daysafter;
$year = date("Y")-$yearsago;
$entries = atd_get_old_posts( $start_ago, $end_ago, $limit);
if (!entries)
$outputlist .= atd_format_old_posts($entries, $year, $none, $showyear, $showdate, $dateformat, $showexcerpt);
break;
case 2: // last x years mode
for($year = 1; $year <= $lastxyears; $year++) {
$start_ago = (365*$year)+$daysbefore;
$end_ago = (365*$year)-$daysafter;
$year = (date("Y")-$year);
$entries = atd_get_old_posts( $start_ago, $end_ago, $limit);
$outputlist .= atd_format_old_posts($entries, $year, $none, $showyear, $showdate, $dateformat, $showexcerpt); }
break;
case 3: // since year x mode
for($year = 1; $year <= (date("Y")-$sinceyear); $year++) {
$start_ago = (365*$year)+$daysbefore;
$end_ago = (365*$year)-$daysafter;
$year = (date("Y")-$year);
$entries = atd_get_old_posts( $start_ago, $end_ago, $limit);
$outputlist .= atd_format_old_posts($entries, $year, $none, $showyear, $showdate, $dateformat, $showexcerpt); }
break;
}
$outputlist .= ' ';
echo $outputlist;
}
function atd_get_old_posts( $start_ago, $end_ago, $limit ) {
global $wpdb;
global $wp_version;
$q = "SELECT ID, post_title, post_date, post_excerpt, post_content FROM $wpdb->posts ";
$q .= " WHERE post_status = 'publish'";
if (version_compare($wp_version, '2.1', '>='))
$q .= " AND post_type = 'post'";
$q .= $wpdb->prepare(" AND (( TO_DAYS( NOW() ) - TO_DAYS( post_date ) ) BETWEEN %d AND %d )", $end_ago, $start_ago);
$q .= " ORDER BY post_date ASC";
$q .= $wpdb->prepare(" LIMIT %d", $limit);
$entries = $wpdb->get_results($q);
return $entries;
}
function atd_format_old_posts( $entries, $year, $none, $showyear, $showdate, $dateformat, $showexcerpt ) {
$output = ''; // empty the 'output' string
$output .= '';
if ($showyear) {
$output .= $year;
}
$output .= '';
if (!empty($entries))
{
foreach ($entries as $entry) {
$title = str_replace('"', '',$entry->post_title);
$title = htmlspecialchars($title);
if($showdate) {
$postdate = ' - ' .mysql2date($dateformat,$entry->post_date). ' ';
} else {
$postdate = '';
}
if($showexcerpt) {
if (empty($entry->post_excerpt))
{
$entry->post_excerpt = explode(" ",strrev(substr(strip_tags($entry->post_content), 0, 100)),2);
$entry->post_excerpt = strrev($entry->post_excerpt[1]);
$entry->post_excerpt.= " [...]";
}
$postexcerpt = htmlspecialchars($entry->post_excerpt);
$postexcerpt = '' . $postexcerpt . ' ';
} else {
$postexcerpt = '';
}
$classes = ' atd-y'. mysql2date('Y',$entry->post_date). ' atd-m'. mysql2date('m',$entry->post_date). ' atd-d' .
mysql2date('d',$entry->post_date) . ' atd-' . mysql2date('Ymd',$entry->post_date) . '';
$output .= '' . htmlspecialchars($entry->post_title) . ' ' . $postdate . ' ' . $postexcerpt . ' ';
}
} else {
$output .= '' . $none. ' ';
}
$output .= ' ';
$output .= ' ';
return $output;
}
/* Widget options */
function widget_atd_options() {
$defaults = array(
'title' => 'This week last year...',
'daysbefore' => 3,
'daysafter' => 3,
'mode' => 1,
'yearsago' => 1,
'lastxyears' => 1,
'sinceyear' => 2006,
'limit' => 4,
'none' => 'none',
'showyear' => true,
'showdate' => false,
'dateformat' => 'F j',
'showexcerpt' => false
);
$options = (array) get_option('widget_atd');
foreach ( $defaults as $key => $value )
if ( !isset($options[$key]) )
$options[$key] = $defaults[$key];
return $options;
}
function widget_atd_display( $args ) {
extract( $args );
$options = widget_atd_options();
echo $before_widget . $before_title . $options['title'] . $after_title;
around_this_date( $options['daysbefore'], $options['daysafter'], $options['mode'], $options['yearsago'], $options['lastxyears'],
$options['sinceyear'], $options['limit'], $options['none'], $options['showyear'], $options['showdate'], $options['dateformat'],
$options['showexcerpt'] );
echo $after_widget;
}
function widget_atd_control() {
$options = $newoptions = widget_atd_options();
if ($_POST['atd-submit'])
{
$newoptions['title'] = strip_tags(stripslashes($_POST['atd-title']));
$newoptions['daysbefore'] = strip_tags(stripslashes($_POST['atd-daysbefore']));
$newoptions['daysafter'] = strip_tags(stripslashes($_POST['atd-daysafter']));
$newoptions['mode'] = strip_tags(stripslashes($_POST['atd-mode']));
$newoptions['yearsago'] = strip_tags(stripslashes($_POST['atd-yearsago']));
$newoptions['sinceyear'] = strip_tags(stripslashes($_POST['atd-sinceyear']));
$newoptions['lastxyears'] = strip_tags(stripslashes($_POST['atd-lastxyears']));
$newoptions['limit'] = strip_tags(stripslashes($_POST['atd-limit']));
$newoptions['none'] = strip_tags(stripslashes($_POST['atd-none']));
$newoptions['showyear'] = isset($_POST['atd-showyear']);
$newoptions['showdate'] = isset($_POST['atd-showdate']);
$newoptions['dateformat'] = strip_tags(stripslashes($_POST['atd-dateformat']));
$newoptions['showexcerpt'] = isset($_POST['atd-showexcerpt']);
}
if ( $options != $newoptions )
{
$options = $newoptions;
update_option('widget_atd', $options);
}
$title = attribute_escape($options['title']);
$daysbefore = (int) $options['daysbefore'];
$daysafter = (int) $options['daysafter'];
$mode = (int) $options['mode'];
$yearsago = (int) $options['yearsago'];
$sinceyear = (int) $options['sinceyear'];
$lastxyears = (int) $options['lastxyears'];
$limit = (int) $options['limit'];
$none = attribute_escape($options['none']);
$showyear = ($options['showyear'] ? 'checked="checked"' : '');
$showdate = ($options['showdate'] ? 'checked="checked"' : '');
$dateformat = attribute_escape($options['dateformat']);
$showexcerpt = ($options['showexcerpt'] ? 'checked="checked"' : '');
?>
Basic options:
Title:
Days before:
Days after:
Limit of entries:
Text when no entries:
Select Mode: $i"; ?>
mode 1: get posts around this date from X years ago.
mode 2: get posts around this date from the last X years.
mode 3: get posts around this date since year X.
Mode Options:
Advanced options:
Show excerpt:
Show year subtitle:
Show post date:
Date format: See PHP Date manual