WordPress.org

Make WordPress Core

Changeset 37685


Ignore:
Timestamp:
06/11/2016 01:30:23 PM (3 years ago)
Author:
ocean90
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/link-template.php

    r37564 r37685  
    35403540
    35413541/**
     3542 * Returns the canonical URL for a post.
     3543 *
     3544 * When the post is the same as the current requested page the function will handle the
     3545 * pagination arguments too.
     3546 *
     3547 * @since 4.6.0
     3548 *
     3549 * @param int|WP_Post $post Optional. Post ID or object. Default is global `$post`.
     3550 * @return string|false The canonical URL, or false if the post does not exist or has not
     3551 *                      been published yet.
     3552 */
     3553function wp_get_canonical_url( $post = null ) {
     3554    $post = get_post( $post );
     3555
     3556    if ( ! $post ) {
     3557        return false;
     3558    }
     3559
     3560    if ( 'publish' !== $post->post_status ) {
     3561        return false;
     3562    }
     3563
     3564    $canonical_url = get_permalink( $post );
     3565
     3566    // If a canonical is being generated for the current page, make sure it has pagination if needed.
     3567    if ( $post->ID === get_queried_object_id() ) {
     3568        $page = get_query_var( 'page', 0 );
     3569        if ( $page >= 2 ) {
     3570            if ( '' == get_option( 'permalink_structure' ) ) {
     3571                $canonical_url = add_query_arg( 'page', $page, $canonical_url );
     3572            } else {
     3573                $canonical_url = trailingslashit( $canonical_url ) . user_trailingslashit( $page, 'single_paged' );
     3574            }
     3575        }
     3576
     3577        $cpage = get_query_var( 'cpage', 0 );
     3578        if ( $cpage ) {
     3579            $canonical_url = get_comments_pagenum_link( $cpage );
     3580        }
     3581    }
     3582
     3583    /**
     3584     * Filters the canonical URL for a post.
     3585     *
     3586     * @since 4.6.0
     3587     *
     3588     * @param string  $string The post's canonical URL.
     3589     * @param WP_Post $post   Post object.
     3590     */
     3591    return apply_filters( 'get_canonical_url', $canonical_url, $post );
     3592}
     3593
     3594/**
    35423595 * Outputs rel=canonical for singular queries.
    35433596 *
    35443597 * @since 2.9.0
     3598 * @since 4.6.0 Adjusted to use `wp_get_canonical_url()`.
    35453599 */
    35463600function rel_canonical() {
     
    35493603    }
    35503604
    3551     if ( ! $id = get_queried_object_id() ) {
     3605    $id = get_queried_object_id();
     3606
     3607    if ( 0 === $id ) {
    35523608        return;
    35533609    }
    35543610
    3555     $url = get_permalink( $id );
    3556 
    3557     $page = get_query_var( 'page' );
    3558     if ( $page >= 2 ) {
    3559         if ( '' == get_option( 'permalink_structure' ) ) {
    3560             $url = add_query_arg( 'page', $page, $url );
    3561         } else {
    3562             $url = trailingslashit( $url ) . user_trailingslashit( $page, 'single_paged' );
    3563         }
    3564     }
    3565 
    3566     $cpage = get_query_var( 'cpage' );
    3567     if ( $cpage ) {
    3568         $url = get_comments_pagenum_link( $cpage );
    3569     }
    3570     echo '<link rel="canonical" href="' . esc_url( $url ) . "\" />\n";
     3611    $url = wp_get_canonical_url( $id );
     3612
     3613    if ( ! empty( $url ) ) {
     3614        echo '<link rel="canonical" href="' . esc_url( $url ) . '" />' . "\n";
     3615    }
    35713616}
    35723617
Note: See TracChangeset for help on using the changeset viewer.