1

I am completely new in using apply_filters() while making a theme. I use a standard Wordpress widget to display calendar. However i need to change his HTML output to get a link button to current month in certain place. This is how I found the apply_filters() function but i don't know how to use it.

Do am i right I can achieve the desired effect with it?

Original function get_calendar() have a part of code below:

if ( $next ) {
    $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="' . get_month_link( $next->year, $next->month ) . '">' . $wp_locale->get_month_abbrev( $wp_locale->get_month( $next->month ) ) . ' &raquo;</a></span>';
} else {
    $calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next">&nbsp;</span>';
}

What i need is to change else outuput to :

$calendar_output .= "\n\t\t" . '<span class="wp-calendar-nav-next"><a href="#">My link</a></span>';

I know that if I duplicate get_calendar() to my function.php i will be able to do anything I want, but i want to try make this by using any filter or hook if the Wordpress gives us such possibility.

Thank You in advance for your response.

1 Answer 1

2

The get_calendar filter hook will allow you to modify the HTML generated by get_calendar() prior to displaying it. Duplicating the get_calendar() function in your functions.php file will throw a fatal error, since you'll be redeclaring an existing function. (You can get around this by calling it something else, like function wpse410569_get_calendar(...), but I'd recommend using the filter hook, since that's what it's there for.)

To modify the output the way you need, add this to your active theme's functions.php file:

add_filter( 'get_calendar', 'wpse410569_modify_calendar' );
function wpse410569_modify_calendar( $calendar_output ) {
    $search  = '<span class="wp-calendar-nav-next">&nbsp;</span>';
    $replace = '<span class="wp-calendar-nav-next"><a href="#">My link</a></span>';
    $calendar_output = str_replace( $search, $replace, $calendar_output );
    return $calendar_output;
}

To learn more about how filter hooks work, consult Filters in the Developer Handbook and/or check out the Filter Hook tutorial video.

3
  • 2
    Might add that the general flow of filters in WP is to pass a value for filtering to the hooked callback function, which in this case, is the calendar markup.
    – jdm2112
    Oct 19, 2022 at 16:28
  • Thank You very much. I am really disappointed by myself because i did not get idea of using str_replace(). Definitely recently there is something wrong with my mind, i am getting old.
    – X9DESIGN
    Oct 19, 2022 at 21:20
  • By default, the calendar is missing something on the first call. It loads the calendar with the current month, so to get all comments from the current month, by the navigation at the very bottom you must first go to the previous one and go back to the current. So I did something like this: $ replace = '<span class =" wp-calendar-nav-next "><a href="'. wp_date('/Y/m').'">'. wp_date ('F'). '</a> </span>' :)
    – X9DESIGN
    Oct 19, 2022 at 21:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.