How to Modify Calendar Widget?
-
Hello, I am trying to modify the wordpress calendar widget, to add links “prev year” and “next year”, and that way you can change the year more easily without having to move 12 months to change year, but I don’t know how I can do it.
Please, can you help me? I guess I have to modify some widget file, but I don’t know which one, and I don’t know the code I have to add.
Thank you very much
-
Hey there,
It is not a native feature in wordpress. It seems that you’re talking about your plugin. I would be able to assist you if you could tell the plugin name you are using.
Thanks!
Hello,
? I’m actually talking about the Widget called Calendar that comes by default in WordPress and can be added in the Sidebar. It is not an external plugin …
I think I have located the file and the code that needs to be modified, but I do not know what code to insert to get a previous and next link for the year …
I guess the file is as follows:
wp-includes->general-template.phpAnd the code fragment that would have to be modified I think is this:
/**
* Display calendar with days that have posts as links.
*
* The calendar is cached, which will be retrieved, if it exists. If there are
* no posts for the month, then it will not be displayed.
*
* @since 1.0.0
*
* @global wpdb $wpdb
* @global int $m
* @global int $monthnum
* @global int $year
* @global WP_Locale $wp_locale
* @global array $posts
*
* @param bool $initial Optional, default is true. Use initial calendar names.
* @param bool $echo Optional, default is true. Set to false for return.
* @return string|void String when retrieving.
*/
function get_calendar( $initial = true, $echo = true ) {
global $wpdb, $m, $monthnum, $year, $wp_locale, $posts;$key = md5( $m . $monthnum . $year );
$cache = wp_cache_get( ‘get_calendar’, ‘calendar’ );if ( $cache && is_array( $cache ) && isset( $cache[ $key ] ) ) {
/** This filter is documented in wp-includes/general-template.php */
$output = apply_filters( ‘get_calendar’, $cache[ $key ] );if ( $echo ) {
echo $output;
return;
}return $output;
}if ( ! is_array( $cache ) ) {
$cache = array();
}// Quick check. If we have no posts at all, abort!
if ( ! $posts ) {
$gotsome = $wpdb->get_var(“SELECT 1 as test FROM $wpdb->posts WHERE post_type = ‘post’ AND post_status = ‘publish’ LIMIT 1″);
if ( ! $gotsome ) {
$cache[ $key ] = ”;
wp_cache_set( ‘get_calendar’, $cache, ‘calendar’ );
return;
}
}if ( isset( $_GET[‘w’] ) ) {
$w = (int) $_GET[‘w’];
}
// week_begins = 0 stands for Sunday
$week_begins = (int) get_option( ‘start_of_week’ );
$ts = current_time( ‘timestamp’ );// Let’s figure out when we are
if ( ! empty( $monthnum ) && ! empty( $year ) ) {
$thismonth = zeroise( intval( $monthnum ), 2 );
$thisyear = (int) $year;
} elseif ( ! empty( $w ) ) {
// We need to get the month from MySQL
$thisyear = (int) substr( $m, 0, 4 );
//it seems MySQL’s weeks disagree with PHP’s
$d = ( ( $w – 1 ) * 7 ) + 6;
$thismonth = $wpdb->get_var(“SELECT DATE_FORMAT((DATE_ADD(‘{$thisyear}0101’, INTERVAL $d DAY) ), ‘%m’)”);
} elseif ( ! empty( $m ) ) {
$thisyear = (int) substr( $m, 0, 4 );
if ( strlen( $m ) < 6 ) {
$thismonth = ’01’;
} else {
$thismonth = zeroise( (int) substr( $m, 4, 2 ), 2 );
}
} else {
$thisyear = gmdate( ‘Y’, $ts );
$thismonth = gmdate( ‘m’, $ts );
}$unixmonth = mktime( 0, 0 , 0, $thismonth, 1, $thisyear );
$last_day = date( ‘t’, $unixmonth );// Get the next and previous month and year with at least one post
$previous = $wpdb->get_row(“SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < ‘$thisyear-$thismonth-01’
AND post_type = ‘post’ AND post_status = ‘publish’
ORDER BY post_date DESC
LIMIT 1”);
$next = $wpdb->get_row(“SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > ‘$thisyear-$thismonth-{$last_day} 23:59:59’
AND post_type = ‘post’ AND post_status = ‘publish’
ORDER BY post_date ASC
LIMIT 1”);
// Get the next and previous month and year with at least one post José ángel
$previousyear = $wpdb->get_row(“SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date < ‘$thisyear-0001-$thismonth’
AND post_type = ‘post’ AND post_status = ‘publish’
ORDER BY post_date DESC
LIMIT 1”);
$nextyear = $wpdb->get_row(“SELECT MONTH(post_date) AS month, YEAR(post_date) AS year
FROM $wpdb->posts
WHERE post_date > ‘$thisyear-$thismonth-{$last_day} 23:59:59’
AND post_type = ‘post’ AND post_status = ‘publish’
ORDER BY post_date ASC
LIMIT 1″);
/* translators: Calendar caption: 1: month name, 2: 4-digit year */
$calendar_caption = _x(‘%1$s %2$s’, ‘calendar caption’);
$calendar_output = ‘<table id=”wp-calendar”>
<caption>’ . sprintf(
$calendar_caption,
$wp_locale->get_month( $thismonth ),
date( ‘Y’, $unixmonth )
) . ‘</caption>
<thead>
<tr>’;$myweek = array();
for ( $wdcount = 0; $wdcount <= 6; $wdcount++ ) {
$myweek[] = $wp_locale->get_weekday( ( $wdcount + $week_begins ) % 7 );
}foreach ( $myweek as $wd ) {
$day_name = $initial ? $wp_locale->get_weekday_initial( $wd ) : $wp_locale->get_weekday_abbrev( $wd );
$wd = esc_attr( $wd );
$calendar_output .= “\n\t\t<th scope=\”col\” title=\”$wd\”>$day_name</th>”;
}$calendar_output .= ‘
</tr>
</thead><tfoot>
<tr>’;if ( $previous ) {
$calendar_output .= “\n\t\t”.'<td colspan=”3″ id=”prev”>year, $previous->month) . ‘” title=”‘ . esc_attr( sprintf(__(‘View posts for %1$s %2$s’), $wp_locale->get_month($previous->month), date(‘Y’, mktime(0, 0 , 0, $previous->month, 1, $previous->year)))) . ‘”>« ‘ . $wp_locale->get_month_abbrev($wp_locale->get_month($previous->month)) . ‘</td>’;
} else {
$calendar_output .= “\n\t\t”.'<td colspan=”3″ id=”prev” class=”pad”> </td>’;
}$calendar_output .= “\n\t\t”.'<td class=”pad”> </td>’;
if ( $next ) {
$calendar_output .= “\n\t\t”.'<td colspan=”3″ id=”next”>year, $next->month) . ‘” title=”‘ . esc_attr( sprintf(__(‘View posts for %1$s %2$s’), $wp_locale->get_month($next->month), date(‘Y’, mktime(0, 0 , 0, $next->month, 1, $next->year))) ) . ‘”>’ . $wp_locale->get_month_abbrev($wp_locale->get_month($next->month)) . ‘ »</td>’;
} else {
$calendar_output .= “\n\t\t”.'<td colspan=”3″ id=”next” class=”pad”> </td>’;
}$calendar_output .= ‘
</tr></tfoot>
<tbody>
<tr>’;$daywithpost = array();
// Get days with posts
$dayswithposts = $wpdb->get_results(“SELECT DISTINCT DAYOFMONTH(post_date)
FROM $wpdb->posts WHERE post_date >= ‘{$thisyear}-{$thismonth}-01 00:00:00’
AND post_type = ‘post’ AND post_status = ‘publish’
AND post_date <= ‘{$thisyear}-{$thismonth}-{$last_day} 23:59:59′”, ARRAY_N);
if ( $dayswithposts ) {
foreach ( (array) $dayswithposts as $daywith ) {
$daywithpost[] = $daywith[0];
}
}// See how much we should pad in the beginning
$pad = calendar_week_mod( date( ‘w’, $unixmonth ) – $week_begins );
if ( 0 != $pad ) {
$calendar_output .= “\n\t\t”.'<td colspan=”‘. esc_attr( $pad ) .'” class=”pad”> </td>’;
}$newrow = false;
$daysinmonth = (int) date( ‘t’, $unixmonth );for ( $day = 1; $day <= $daysinmonth; ++$day ) {
if ( isset($newrow) && $newrow ) {
$calendar_output .= “\n\t</tr>\n\t<tr>\n\t\t”;
}
$newrow = false;if ( $day == gmdate( ‘j’, $ts ) &&
$thismonth == gmdate( ‘m’, $ts ) &&
$thisyear == gmdate( ‘Y’, $ts ) ) {
$calendar_output .= ‘<td id=”today”>’;
} else {
$calendar_output .= ‘<td>’;
}if ( in_array( $day, $daywithpost ) ) {
// any posts today?
$date_format = date( _x( ‘F j, Y’, ‘daily archives date format’ ), strtotime( “{$thisyear}-{$thismonth}-{$day}” ) );
/* translators: Post calendar label. 1: Date */
$label = sprintf( __( ‘Posts published on %s’ ), $date_format );
$calendar_output .= sprintf(
‘%s‘,
get_day_link( $thisyear, $thismonth, $day ),
esc_attr( $label ),
$day
);
} else {
$calendar_output .= $day;
}
$calendar_output .= ‘</td>’;if ( 6 == calendar_week_mod( date( ‘w’, mktime(0, 0 , 0, $thismonth, $day, $thisyear ) ) – $week_begins ) ) {
$newrow = true;
}
}$pad = 7 – calendar_week_mod( date( ‘w’, mktime( 0, 0 , 0, $thismonth, $day, $thisyear ) ) – $week_begins );
if ( $pad != 0 && $pad != 7 ) {
$calendar_output .= “\n\t\t”.'<td class=”pad” colspan=”‘. esc_attr( $pad ) .'”> </td>’;
}
$calendar_output .= “\n\t</tr>\n\t</tbody>\n\t</table>”;$cache[ $key ] = $calendar_output;
wp_cache_set( ‘get_calendar’, $cache, ‘calendar’ );if ( $echo ) {
/**
* Filters the HTML calendar output.
*
* @since 3.0.0
*
* @param string $calendar_output HTML output of the calendar.
*/
echo apply_filters( ‘get_calendar’, $calendar_output );
return;
}
/** This filter is documented in wp-includes/general-template.php */
return apply_filters( ‘get_calendar’, $calendar_output );
}/**
* Purge the cached results of get_calendar.
*
* @see get_calendar
* @since 2.1.0
*/
function delete_get_calendar_cache() {
wp_cache_delete( ‘get_calendar’, ‘calendar’ );
}/**
* Display all of the allowed tags in HTML format with attributes.
*
* This is useful for displaying in the comment area, which elements and
* attributes are supported. As well as any plugins which want to display it.
*
* @since 1.0.1
*
* @global array $allowedtags
*
* @return string HTML allowed tags entity encoded.
*/
function allowed_tags() {
global $allowedtags;
$allowed = ”;
foreach ( (array) $allowedtags as $tag => $attributes ) {
$allowed .= ‘<‘.$tag;
if ( 0 < count($attributes) ) {
foreach ( $attributes as $attribute => $limits ) {
$allowed .= ‘ ‘.$attribute.’=””‘;
}
}
$allowed .= ‘> ‘;
}
return htmlentities( $allowed );
}/***** Date/Time tags *****/
/**
* Outputs the date in iso8601 format for xml files.
*
* @since 1.0.0
*/
function the_date_xml() {
echo mysql2date( ‘Y-m-d’, get_post()->post_date, false );
}/**
* Display or Retrieve the date the current post was written (once per date)
*
* Will only output the date if the current post’s date is different from the
* previous one output.
*
* i.e. Only one date listing will show per day worth of posts shown in the loop, even if the
* function is called several times for each post.
*
* HTML output can be filtered with ‘the_date’.
* Date string output can be filtered with ‘get_the_date’.
*
* @since 0.71
*
* @global string|int|bool $currentday
* @global string|int|bool $previousday
*
* @param string $d Optional. PHP date format defaults to the date_format option if not specified.
* @param string $before Optional. Output before the date.
* @param string $after Optional. Output after the date.
* @param bool $echo Optional, default is display. Whether to echo the date or return it.
* @return string|void String if retrieving.
*/
function the_date( $d = ”, $before = ”, $after = ”, $echo = true ) {
global $currentday, $previousday;if ( is_new_day() ) {
$the_date = $before . get_the_date( $d ) . $after;
$previousday = $currentday;/**
* Filters the date a post was published for display.
*
* @since 0.71
*
* @param string $the_date The formatted date string.
* @param string $d PHP date format. Defaults to ‘date_format’ option
* if not specified.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_date = apply_filters( ‘the_date’, $the_date, $d, $before, $after );if ( $echo )
echo $the_date;
else
return $the_date;
}
}/**
* Retrieve the date on which the post was written.
*
* Unlike the_date() this function will always return the date.
* Modify output with the {@see ‘get_the_date’} filter.
*
* @since 3.0.0
*
* @param string $d Optional. PHP date format defaults to the date_format option if not specified.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
* @return false|string Date the current post was written. False on failure.
*/
function get_the_date( $d = ”, $post = null ) {
$post = get_post( $post );if ( ! $post ) {
return false;
}if ( ” == $d ) {
$the_date = mysql2date( get_option( ‘date_format’ ), $post->post_date );
} else {
$the_date = mysql2date( $d, $post->post_date );
}/**
* Filters the date a post was published.
*
* @since 3.0.0
*
* @param string $the_date The formatted date.
* @param string $d PHP date format. Defaults to ‘date_format’ option
* if not specified.
* @param int|WP_Post $post The post object or ID.
*/
return apply_filters( ‘get_the_date’, $the_date, $d, $post );
}/**
* Display the date on which the post was last modified.
*
* @since 2.1.0
*
* @param string $d Optional. PHP date format defaults to the date_format option if not specified.
* @param string $before Optional. Output before the date.
* @param string $after Optional. Output after the date.
* @param bool $echo Optional, default is display. Whether to echo the date or return it.
* @return string|void String if retrieving.
*/
function the_modified_date( $d = ”, $before = ”, $after = ”, $echo = true ) {
$the_modified_date = $before . get_the_modified_date($d) . $after;/**
* Filters the date a post was last modified for display.
*
* @since 2.1.0
*
* @param string $the_modified_date The last modified date.
* @param string $d PHP date format. Defaults to ‘date_format’ option
* if not specified.
* @param string $before HTML output before the date.
* @param string $after HTML output after the date.
*/
$the_modified_date = apply_filters( ‘the_modified_date’, $the_modified_date, $d, $before, $after );if ( $echo )
echo $the_modified_date;
else
return $the_modified_date;}
/**
* Retrieve the date on which the post was last modified.
*
* @since 2.1.0
* @since 4.6.0 Added the$post
parameter.
*
* @param string $d Optional. PHP date format defaults to the date_format option if not specified.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
* @return false|string Date the current post was modified. False on failure.
*/
function get_the_modified_date( $d = ”, $post = null ) {
$post = get_post( $post );if ( ! $post ) {
// For backward compatibility, failures go through the filter below.
$the_time = false;
} elseif ( empty( $d ) ) {
$the_time = get_post_modified_time( get_option( ‘date_format’ ), false, $post, true );
} else {
$the_time = get_post_modified_time( $d, false, $post, true );
}/**
* Filters the date a post was last modified.
*
* @since 2.1.0
* @since 4.6.0 Added the$post
parameter.
*
* @param string $the_time The formatted date.
* @param string $d PHP date format. Defaults to value specified in
* ‘date_format’ option.
* @param WP_Post $post WP_Post object.
*/
return apply_filters( ‘get_the_modified_date’, $the_time, $d, $post );
}/**
* Display the time at which the post was written.
*
* @since 0.71
*
* @param string $d Either ‘G’, ‘U’, or php date format.
*/
function the_time( $d = ” ) {
/**
* Filters the time a post was written for display.
*
* @since 0.71
*
* @param string $get_the_time The formatted time.
* @param string $d The time format. Accepts ‘G’, ‘U’,
* or php date format.
*/
echo apply_filters( ‘the_time’, get_the_time( $d ), $d );
}/**
* Retrieve the time at which the post was written.
*
* @since 1.5.0
*
* @param string $d Optional. Format to use for retrieving the time the post
* was written. Either ‘G’, ‘U’, or php date format defaults
* to the value specified in the time_format option. Default empty.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @return string|int|false Formatted date string or Unix timestamp if$id
is ‘U’ or ‘G’. False on failure.
*/
function get_the_time( $d = ”, $post = null ) {
$post = get_post($post);if ( ! $post ) {
return false;
}if ( ” == $d )
$the_time = get_post_time(get_option(‘time_format’), false, $post, true);
else
$the_time = get_post_time($d, false, $post, true);/**
* Filters the time a post was written.
*
* @since 1.5.0
*
* @param string $the_time The formatted time.
* @param string $d Format to use for retrieving the time the post was written.
* Accepts ‘G’, ‘U’, or php date format value specified
* in ‘time_format’ option. Default empty.
* @param int|WP_Post $post WP_Post object or ID.
*/
return apply_filters( ‘get_the_time’, $the_time, $d, $post );
}/**
* Retrieve the time at which the post was written.
*
* @since 2.0.0
*
* @param string $d Optional. Format to use for retrieving the time the post
* was written. Either ‘G’, ‘U’, or php date format. Default ‘U’.
* @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @param bool $translate Whether to translate the time string. Default false.
* @return string|int|false Formatted date string or Unix timestamp if$id
is ‘U’ or ‘G’. False on failure.
*/
function get_post_time( $d = ‘U’, $gmt = false, $post = null, $translate = false ) {
$post = get_post($post);if ( ! $post ) {
return false;
}if ( $gmt )
$time = $post->post_date_gmt;
else
$time = $post->post_date;$time = mysql2date($d, $time, $translate);
/**
* Filters the localized time a post was written.
*
* @since 2.6.0
*
* @param string $time The formatted time.
* @param string $d Format to use for retrieving the time the post was written.
* Accepts ‘G’, ‘U’, or php date format. Default ‘U’.
* @param bool $gmt Whether to retrieve the GMT time. Default false.
*/
return apply_filters( ‘get_post_time’, $time, $d, $gmt );
}/**
* Display the time at which the post was last modified.
*
* @since 2.0.0
*
* @param string $d Optional Either ‘G’, ‘U’, or php date format defaults to the value specified in the time_format option.
*/
function the_modified_time($d = ”) {
/**
* Filters the localized time a post was last modified, for display.
*
* @since 2.0.0
*
* @param string $get_the_modified_time The formatted time.
* @param string $d The time format. Accepts ‘G’, ‘U’,
* or php date format. Defaults to value
* specified in ‘time_format’ option.
*/
echo apply_filters( ‘the_modified_time’, get_the_modified_time($d), $d );
}/**
* Retrieve the time at which the post was last modified.
*
* @since 2.0.0
* @since 4.6.0 Added the$post
parameter.
*
* @param string $d Optional. Format to use for retrieving the time the post
* was modified. Either ‘G’, ‘U’, or php date format defaults
* to the value specified in the time_format option. Default empty.
* @param int|WP_Post $post Optional. Post ID or WP_Post object. Default current post.
* @return false|string Formatted date string or Unix timestamp. False on failure.
*/
function get_the_modified_time( $d = ”, $post = null ) {
$post = get_post( $post );if ( ! $post ) {
// For backward compatibility, failures go through the filter below.
$the_time = false;
} elseif ( empty( $d ) ) {
$the_time = get_post_modified_time( get_option( ‘time_format’ ), false, $post, true );
} else {
$the_time = get_post_modified_time( $d, false, $post, true );
}/**
* Filters the localized time a post was last modified.
*
* @since 2.0.0
* @since 4.6.0 Added the$post
parameter.
*
* @param string $the_time The formatted time.
* @param string $d Format to use for retrieving the time the post was
* written. Accepts ‘G’, ‘U’, or php date format. Defaults
* to value specified in ‘time_format’ option.
* @param WP_Post $post WP_Post object.
*/
return apply_filters( ‘get_the_modified_time’, $the_time, $d, $post );
}/**
* Retrieve the time at which the post was last modified.
*
* @since 2.0.0
*
* @param string $d Optional. Format to use for retrieving the time the post
* was modified. Either ‘G’, ‘U’, or php date format. Default ‘U’.
* @param bool $gmt Optional. Whether to retrieve the GMT time. Default false.
* @param int|WP_Post $post WP_Post object or ID. Default is global $post object.
* @param bool $translate Whether to translate the time string. Default false.
* @return string|int|false Formatted date string or Unix timestamp if$id
is ‘U’ or ‘G’. False on failure.
*/
function get_post_modified_time( $d = ‘U’, $gmt = false, $post = null, $translate = false ) {
$post = get_post($post);if ( ! $post ) {
return false;
}if ( $gmt )
$time = $post->post_modified_gmt;
else
$time = $post->post_modified;
$time = mysql2date($d, $time, $translate);/**
* Filters the localized time a post was last modified.
*
* @since 2.8.0
*
* @param string $time The formatted time.
* @param string $d The date format. Accepts ‘G’, ‘U’, or php date format. Default ‘U’.
* @param bool $gmt Whether to return the GMT time. Default false.
*/
return apply_filters( ‘get_post_modified_time’, $time, $d, $gmt );
}/**
* Display the weekday on which the post was written.
*
* @since 0.71
*
* @global WP_Locale $wp_locale
*/
function the_weekday() {
global $wp_locale;
$the_weekday = $wp_locale->get_weekday( mysql2date( ‘w’, get_post()->post_date, false ) );/**
* Filters the weekday on which the post was written, for display.
*
* @since 0.71
*
* @param string $the_weekday
*/
echo apply_filters( ‘the_weekday’, $the_weekday );
}/**
* Display the weekday on which the post was written.
*
* Will only output the weekday if the current post’s weekday is different from
* the previous one output.
*
* @since 0.71
*
* @global WP_Locale $wp_locale
* @global string|int|bool $currentday
* @global string|int|bool $previousweekday
*
* @param string $before Optional Output before the date.
* @param string $after Optional Output after the date.
*/
function the_weekday_date($before=”,$after=”) {
global $wp_locale, $currentday, $previousweekday;
$the_weekday_date = ”;
if ( $currentday != $previousweekday ) {
$the_weekday_date .= $before;
$the_weekday_date .= $wp_locale->get_weekday( mysql2date( ‘w’, get_post()->post_date, false ) );
$the_weekday_date .= $after;
$previousweekday = $currentday;
}/**
* Filters the localized date on which the post was written, for display.
*
* @since 0.71
*
* @param string $the_weekday_date
* @param string $before The HTML to output before the date.
* @param string $after The HTML to output after the date.
*/
$the_weekday_date = apply_filters( ‘the_weekday_date’, $the_weekday_date, $before, $after );
echo $the_weekday_date;
}/**
- The topic ‘How to Modify Calendar Widget?’ is closed to new replies.