Using WordPress ‘get_calendar’ PHP action

The get_calendar WordPress PHP filter allows you to modify the HTML output of the calendar.

Usage

add_filter('get_calendar', 'my_custom_calendar', 10, 1);
function my_custom_calendar($calendar_output) {
    // your custom code here
    return $calendar_output;
}

Parameters

  • $calendar_output (string): The HTML output of the calendar.

More information

See WordPress Developer Resources: get_calendar

Examples

Change the calendar table CSS class

Customize the CSS class of the calendar table.

add_filter('get_calendar', 'change_calendar_css_class', 10, 1);
function change_calendar_css_class($calendar_output) {
    $calendar_output = str_replace('wp-calendar', 'my-custom-calendar', $calendar_output);
    return $calendar_output;
}

Add a custom title to the calendar

Insert a custom title above the calendar.

add_filter('get_calendar', 'add_calendar_title', 10, 1);
function add_calendar_title($calendar_output) {
    $title = '<h3>My Custom Calendar</h3>';
    $calendar_output = $title . $calendar_output;
    return $calendar_output;
}

Remove the navigation links from the calendar.

add_filter('get_calendar', 'hide_calendar_navigation', 10, 1);
function hide_calendar_navigation($calendar_output) {
    $pattern = '/<p id="calendar-nav">.*?<\/p>/s';
    $calendar_output = preg_replace($pattern, '', $calendar_output);
    return $calendar_output;
}

Wrap the calendar in a custom div

Add a custom div around the calendar for styling purposes.

add_filter('get_calendar', 'wrap_calendar_in_custom_div', 10, 1);
function wrap_calendar_in_custom_div($calendar_output) {
    $calendar_output = '<div class="my-custom-calendar-wrapper">' . $calendar_output . '</div>';
    return $calendar_output;
}

Highlight today’s date with a custom CSS class

Add a custom CSS class to highlight today’s date in the calendar.

add_filter('get_calendar', 'highlight_todays_date', 10, 1);
function highlight_todays_date($calendar_output) {
    $today = date('j');
    $pattern = '/<td[^>]*>' . $today . '<\/td>/';
    $replacement = '<td class="today-highlight">' . $today . '</td>';
    $calendar_output = preg_replace($pattern, $replacement, $calendar_output);
    return $calendar_output;
}

Add a custom CSS class to the calendar

Add a custom CSS class to the calendar’s outer <table> element.

add_filter('get_calendar', 'add_custom_calendar_class');
function add_custom_calendar_class($calendar_output) {
    $calendar_output = str_replace('<table id="wp-calendar">', '<table id="wp-calendar" class="my-custom-class">', $calendar_output);
    return $calendar_output;
}

Replace default month abbreviations

Replace the default month abbreviations with custom ones.

add_filter('get_calendar', 'replace_month_abbreviations');
function replace_month_abbreviations($calendar_output) {
    $search = array('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec');
    $replace = array('J', 'F', 'M', 'A', 'M', 'J', 'J', 'A', 'S', 'O', 'N', 'D');
    $calendar_output = str_replace($search, $replace, $calendar_output);
    return $calendar_output;
}

Highlight weekends in the calendar

Highlight weekends (Saturday and Sunday) in the calendar with a custom CSS class.

add_filter('get_calendar', 'highlight_weekends');
function highlight_weekends($calendar_output) {
    $calendar_output = preg_replace('/<td>(\d+)<\/td>/', '<td class="highlight">$1</td>', $calendar_output);
    return $calendar_output;
}

Add custom content before the calendar

Add custom content, such as a title, before the calendar output.

add_filter('get_calendar', 'add_content_before_calendar');
function add_content_before_calendar($calendar_output) {
    $custom_content = '<h2>My Custom Calendar</h2>';
    $calendar_output = $custom_content . $calendar_output;
    return $calendar_output;
}

Remove the previous and next month navigation links from the calendar output.

add_filter('get_calendar', 'remove_calendar_navigation');
function remove_calendar_navigation($calendar_output) {
    $calendar_output = preg_replace('/<span id="[^"]+" class="[^"]+">&laquo;<\/span>/', '', $calendar_output);
    $calendar_output = preg_replace('/<span id="[^"]+" class="[^"]+">&raquo;<\/span>/', '', $calendar_output);
    return $calendar_output;
}