array( 'min_range' => 1, 'max_range' => 12, ), ) ); // Back-compat for legacy `month` query arg if present. if ( false === $month_input || null === $month_input ) { $month_input = filter_input( INPUT_GET, 'month', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1, 'max_range' => 12, ), ) ); } $year_input = filter_input( INPUT_GET, $year_param, FILTER_VALIDATE_INT, array( 'options' => array( // Choose a sane range for years. 'min_range' => 1970, 'max_range' => 2200, ), ) ); // Back-compat for legacy `year` query arg if present. if ( false === $year_input || null === $year_input ) { $year_input = filter_input( INPUT_GET, 'year', FILTER_VALIDATE_INT, array( 'options' => array( 'min_range' => 1970, 'max_range' => 2200, ), ) ); } $curMonth = ( false !== $month_input && null !== $month_input ) ? (int) $month_input : (int) wp_date( 'n' ); $curYear = ( false !== $year_input && null !== $year_input ) ? (int) $year_input : (int) wp_date( 'Y' ); // Get previous and next month and year. $prev_month = $curMonth - 1; $prev_year = $curYear; if ( $prev_month < 1 ) { $prev_month = 12; --$prev_year; } $next_month = $curMonth + 1; $next_year = $curYear; if ( $next_month > 12 ) { $next_month = 1; ++$next_year; } // Get the number of days in the month. $days_in_month = cal_days_in_month( CAL_GREGORIAN, $curMonth, $curYear ); // Get the first day of the month in site timezone. $tz = wp_timezone(); $first_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-01 00:00:00', $curYear, $curMonth ), $tz ) )->getTimestamp(); $first_day = (int) wp_date( 'N', $first_ts ); // Get events for the current month. $events = new WP_Query( array( 'post_type' => 'event', 'posts_per_page' => -1, 'meta_query' => array( array( 'key' => 'event_datetime', 'value' => array( "$curYear-$curMonth-01 00:00:00", "$curYear-$curMonth-$days_in_month 23:59:59" ), 'compare' => 'BETWEEN', 'type' => 'DATETIME', ), ), ) ); $events_by_day = array(); if ( $events->have_posts() ) { while ( $events->have_posts() ) { $events->the_post(); $event_date = get_field( 'event_datetime' ); $event_ts = ( new DateTimeImmutable( $event_date, $tz ) )->getTimestamp(); $day = (int) wp_date( 'j', $event_ts ); $events_by_day[ $day ][] = '' . get_the_title() . ''; } wp_reset_postdata(); } // Get today's date. $todayDay = intval( wp_date( 'j' ) ); $todayMonth = intval( wp_date( 'n' ) ); $todayYear = intval( wp_date( 'Y' ) ); // Helper to check if today is in current view. $isTodayInCurrentMonth = ( $curMonth === $todayMonth && $curYear === $todayYear ); ?> '; echo '
' . esc_html( $day ) . '
'; if ( isset( $eventsByDay[ $day ] ) ) { echo ''; } echo ''; }; // Overwrite the default day cell output in the table loop. ob_start(); for ( $day = 1; $day <= $days_in_month; $day++ ) { if ( ( $day + $first_day - 2 ) % 7 === 0 && $day > 1 ) { echo ''; } $originalEchoDayCell( $day, $first_day, $events_by_day ); } $calendarRows = ob_get_clean(); // Replace the original loop output with the new one. $calendarOutput = preg_replace( '/for\s*\(\s*\$day\s*=\s*1;\s*\$day\s*<=\s*\$days_in_month;\s*\$day\+\+\s*\)\s*\{.*?\}/s', $calendarRows, ob_get_contents() ); ?>
getTimestamp(); $next_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-01 00:00:00', $next_year, $next_month ), $tz ) )->getTimestamp(); $prevMonthName = wp_date( 'F', $prev_ts ); $nextMonthName = wp_date( 'F', $next_ts ); ?> " >«

" > »
'; } // Loop through the days of the month. for ( $day = 1; $day <= $days_in_month; $day++ ) { if ( ( $day + $first_day - 2 ) % 7 === 0 && $day > 1 ) { echo ''; } $cellClass = ''; if ( $isTodayInCurrentMonth && $day === $todayDay ) { $cellClass = 'today-cell'; } $cell_ts = ( new DateTimeImmutable( sprintf( '%04d-%02d-%02d 00:00:00', $curYear, $curMonth, $day ), $tz ) )->getTimestamp(); echo ''; } // Add empty cells for the last week. $remaining_days = 7 - ( ( $days_in_month + $first_day - 1 ) % 7 ); if ( $remaining_days < 7 ) { for ( $i = 0; $i < $remaining_days; $i++ ) { echo ''; } } ?>
'; echo '
' . esc_html( $day ) . '
'; if ( isset( $events_by_day[ $day ] ) ) { echo '
    '; foreach ( $events_by_day[ $day ] as $event ) { echo '
  • ' . wp_kses_post( $event ) . '
  • '; } echo '
'; } echo '