On this page
get_month_link( int|false $year, int|false $month ): string
Retrieves the permalink for the month archives with year.
Parameters
$yearint|false Required-
Integer of year. False for current year.
$monthint|false Required-
Integer of month. False for current month.
Return
string The permalink for the specified month and year archive.
More Information
In a Plugin or Theme, it can be used as early as the setup_theme Action. Any earlier usage, including plugins_loaded, generates a Fatal Error.
Source
File: wp-includes/link-template.php. View all references
function get_month_link( $year, $month ) {
global $wp_rewrite;
if ( ! $year ) {
$year = current_time( 'Y' );
}
if ( ! $month ) {
$month = current_time( 'm' );
}
$monthlink = $wp_rewrite->get_month_permastruct();
if ( ! empty( $monthlink ) ) {
$monthlink = str_replace( '%year%', $year, $monthlink );
$monthlink = str_replace( '%monthnum%', zeroise( (int) $month, 2 ), $monthlink );
$monthlink = home_url( user_trailingslashit( $monthlink, 'month' ) );
} else {
$monthlink = home_url( '?m=' . $year . zeroise( $month, 2 ) );
}
/**
* Filters the month archive permalink.
*
* @since 1.5.0
*
* @param string $monthlink Permalink for the month archive.
* @param int $year Year for the archive.
* @param int $month The month for the archive.
*/
return apply_filters( 'month_link', $monthlink, $year, $month );
}
Hooks
- apply_filters( 'month_link',
string $monthlink ,int $year ,int $month ) -
Filters the month archive permalink.
Related
Uses
| Uses | Description |
|---|---|
| zeroise() wp-includes/formatting.php | Add leading zeros when necessary. |
| current_time() wp-includes/functions.php | Retrieves the current time based on specified type. |
| user_trailingslashit() wp-includes/link-template.php | Retrieves a trailing-slashed string if the site is set for adding trailing slashes. |
| WP_Rewrite::get_month_permastruct() wp-includes/class-wp-rewrite.php | Retrieves the month permalink structure without day and with year. |
| home_url() wp-includes/link-template.php | Retrieves the URL for the current site where the front end is accessible. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
Used By
| Used By | Description |
|---|---|
| wp_get_archives() wp-includes/general-template.php | Displays archive links based on type and format. |
| get_calendar() wp-includes/general-template.php | Displays calendar with days that have posts as links. |
| redirect_canonical() wp-includes/canonical.php | Redirects incoming links to the proper URL based on the site url. |
Changelog
| Version | Description |
|---|---|
| 1.0.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_month_link