Canonical: Introduce `wp_get_canonical_url()`.

`wp_get_canonical_url()` encapsulates the URL logic of `rel_canonical()` to provide a consistent way to retrieve the canonical URL for a post.
The new filter `get_canonical_url` allows to customize the canonical URL.

Props joostdevalk, jipmoors, DrewAPicture, ocean90.
Fixes #36168.
Built from https://develop.svn.wordpress.org/trunk@37685


git-svn-id: http://core.svn.wordpress.org/trunk@37651 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Dominik Schilling 2016-06-11 13:31:29 +00:00
parent f78dd6fe2e
commit e50f357944
2 changed files with 61 additions and 16 deletions

View File

@ -3538,36 +3538,81 @@ function get_edit_profile_url( $user_id = 0, $scheme = 'admin' ) {
return apply_filters( 'edit_profile_url', $url, $user_id, $scheme);
}
/**
* Returns the canonical URL for a post.
*
* When the post is the same as the current requested page the function will handle the
* pagination arguments too.
*
* @since 4.6.0
*
* @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
* @return string|false The canonical URL, or false if the post does not exist or has not
* been published yet.
*/
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 ( $post->ID === get_queried_object_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 $string The post's canonical URL.
* @param WP_Post $post Post object.
*/
return apply_filters( 'get_canonical_url', $canonical_url, $post );
}
/**
* Outputs rel=canonical for singular queries.
*
* @since 2.9.0
* @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
*/
function rel_canonical() {
if ( ! is_singular() ) {
return;
}
if ( ! $id = get_queried_object_id() ) {
$id = get_queried_object_id();
if ( 0 === $id ) {
return;
}
$url = get_permalink( $id );
$url = wp_get_canonical_url( $id );
$page = get_query_var( 'page' );
if ( $page >= 2 ) {
if ( '' == get_option( 'permalink_structure' ) ) {
$url = add_query_arg( 'page', $page, $url );
} else {
$url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
}
if ( ! empty( $url ) ) {
echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
}
$cpage = get_query_var( 'cpage' );
if ( $cpage ) {
$url = get_comments_pagenum_link( $cpage );
}
echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
}
/**

View File

@ -4,7 +4,7 @@
*
* @global string $wp_version
*/
$wp_version = '4.6-alpha-37684';
$wp_version = '4.6-alpha-37685';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.