Advertisement
LABE

[WP] Calendar Widget force prev/next month

Jan 25th, 2017
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.20 KB | None | 0 0
  1. function get_calendar_filter( $input ) {
  2.     global $year, $monthnum, $m, $wp_locale;
  3.     $output = $input;
  4.     // 現在の年月を取得
  5.     $ts = current_time('timestamp');
  6.     $currentyear  = (int) gmdate('Y', $ts);
  7.     $currentmonth = gmdate('m', $ts);
  8.     // グローバル変数から現在のページの年月を取得
  9.     if ( $year && $monthnum ) {
  10.         $thisyear  = (int) $year;
  11.         $thismonth = (int) sprintf( '%02s', $monthnum );
  12.     } elseif ( isset($m) && is_numeric($m) && strlen($m) > 3 ) {
  13.         $thisyear = (int) substr($m, 0, 4);
  14.         if ( strlen($m) < 6 )
  15.             $thismonth = 1;
  16.         else
  17.             $thismonth = (int) substr($m, 4, 2);
  18.     }
  19.     // 年月のチェック
  20.     if ( $thisyear > 1970 && $thismonth > 0 && $thismonth < 13 ) {
  21.         $thisyear  = $thisyear;
  22.         $thismonth = sprintf( '%02s', $thismonth );
  23.     } else { // 不正なら現在の年月を使用
  24.         $thisyear  = $currentyear;
  25.         $thismonth = $currentmonth;
  26.     }
  27.     // 前月・翌月を取得
  28.     if ( $thismonth === '01' ) {
  29.         $prev = array($thisyear - 1, 12);
  30.         $next = array($thisyear,      2);
  31.     } elseif ( $thismonth === '12' ) {
  32.         $prev = array($thisyear,     11);
  33.         $next = array($thisyear + 1,  1);
  34.     } else {
  35.         $prev = array($thisyear, (int) $thismonth - 1 );
  36.         $next = array($thisyear, (int) $thismonth + 1 );
  37.     }
  38.     // 前月を書き換え
  39.     if ( ($currentyear * 100 + $currentmonth) >= ($prev[0] * 100 + $prev[1]) ) { // 未来でなければ
  40.         $prev_pattern = '/<td colspan="3" id="prev"[^>]*>.+?<\/td>/';
  41.         $prev_replace = '<td colspan="3" id="prev"><a href="'.get_month_link($prev[0], $prev[1]).'">&laquo; '
  42.             .$wp_locale->get_month_abbrev($wp_locale->get_month($prev[1])).'</a></td>';
  43.         $output = preg_replace( $prev_pattern, $prev_replace, $output );
  44.     }
  45.     // 翌月を書き換え
  46.     if ( ($currentyear * 100 + $currentmonth) >= ($next[0] * 100 + $next[1]) ) { // 未来でなければ
  47.         $next_pattern = '/<td colspan="3" id="next"[^>]*>.+?<\/td>/';
  48.         $next_replace = '<td colspan="3" id="next"><a href="'.get_month_link($next[0], $next[1]).'">'
  49.             .$wp_locale->get_month_abbrev($wp_locale->get_month($next[1])).' &raquo;</a></td>';
  50.         $output = preg_replace( $next_pattern, $next_replace, $output );
  51.     }
  52.     return $output;
  53. }
  54. add_filter( 'get_calendar', 'get_calendar_filter' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement