The month_link WordPress PHP filter allows you to modify the permalink of a month archive page.
Usage
add_filter('month_link', 'your_custom_function', 10, 3); function your_custom_function($monthlink, $year, $month) { // your custom code here return $monthlink; }
Parameters
- $monthlink (string): The permalink for the month archive.
- $year (int): The year for the archive.
- $month (int): The month for the archive.
More information
See WordPress Developer Resources: month_link
Examples
Add a query string to month archive links
This code appends a custom query string to month archive links.
function add_query_string_to_month_link($monthlink, $year, $month) { $monthlink = add_query_arg('custom_param', 'value', $monthlink); return $monthlink; } add_filter('month_link', 'add_query_string_to_month_link', 10, 3);
Change month archive URL structure
This code changes the month archive URL structure from /year/month
to /archive/year-month
.
function custom_month_archive_url_structure($monthlink, $year, $month) { $monthlink = home_url('/archive/' . $year . '-' . zeroise($month, 2)); return $monthlink; } add_filter('month_link', 'custom_month_archive_url_structure', 10, 3);
Add the archive type to the month archive URL
This code appends the archive type to month archive URLs.
function append_archive_type_to_month_link($monthlink, $year, $month) { $monthlink = trailingslashit($monthlink) . 'archive-type'; return $monthlink; } add_filter('month_link', 'append_archive_type_to_month_link', 10, 3);
Redirect month archives to a custom page
This code redirects all month archive pages to a custom page called “Archives”.
function redirect_month_archives_to_custom_page($monthlink, $year, $month) { $monthlink = home_url('/archives/'); return $monthlink; } add_filter('month_link', 'redirect_month_archives_to_custom_page', 10, 3);
Add a custom prefix to month archive URLs
This code adds a custom prefix to the month archive URLs.
function add_custom_prefix_to_month_link($monthlink, $year, $month) { $monthlink = str_replace('/' . $year, '/custom-prefix/' . $year, $monthlink); return $monthlink; } add_filter('month_link', 'add_custom_prefix_to_month_link', 10, 3);