On this page
wp_get_canonical_url( int|WP_Post $post = null ): string|false
Returns the canonical URL for a post.
Description
When the post is the same as the current requested page the function will handle the pagination arguments too.
Parameters
$postint|WP_Post Optional-
Post ID or object. Default is global
$post.Default:
null
Return
string|false The canonical URL. False if the post does not exist or has not been published yet.
Source
File: wp-includes/link-template.php. View all references
function wp_get_canonical_url( $post = null ) {
$post = get_post( $post );
if ( ! $post ) {
return false;
}
if ( 'publish' !== $post->post_status ) {
return false;
}
$canonical_url = get_permalink( $post );
// If a canonical is being generated for the current page, make sure it has pagination if needed.
if ( get_queried_object_id() === $post->ID ) {
$page = get_query_var( 'page', 0 );
if ( $page >= 2 ) {
if ( ! get_option( 'permalink_structure' ) ) {
$canonical_url = add_query_arg( 'page', $page, $canonical_url );
} else {
$canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
}
}
$cpage = get_query_var( 'cpage', 0 );
if ( $cpage ) {
$canonical_url = get_comments_pagenum_link( $cpage );
}
}
/**
* Filters the canonical URL for a post.
*
* @since 4.6.0
*
* @param string $canonical_url The post's canonical URL.
* @param WP_Post $post Post object.
*/
return apply_filters( 'get_canonical_url', $canonical_url, $post );
}
Hooks
- apply_filters( 'get_canonical_url',
string $canonical_url ,WP_Post $post ) -
Filters the canonical URL for a post.
Related
Uses
| Uses | Description |
|---|---|
| get_queried_object_id() wp-includes/query.php | Retrieves the ID of the currently queried object. |
| get_query_var() wp-includes/query.php | Retrieves the value of a query variable in the WP_Query class. |
| get_comments_pagenum_link() wp-includes/link-template.php | Retrieves the comments page number link. |
| user_trailingslashit() wp-includes/link-template.php | Retrieves a trailing-slashed string if the site is set for adding trailing slashes. |
| trailingslashit() wp-includes/formatting.php | Appends a trailing slash. |
| add_query_arg() wp-includes/functions.php | Retrieves a modified URL query string. |
| get_permalink() wp-includes/link-template.php | Retrieves the full permalink for the current post or post ID. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| rel_canonical() wp-includes/link-template.php | Outputs rel=canonical for singular queries. |
Changelog
| Version | Description |
|---|---|
| 4.6.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_get_canonical_url