public
Created

new rel_canonical function

  • Download Gist
gistfile1.aw
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
<?php
 
// this would replace the rel_canonical function in wp-includes/link-template.php
 
/**
* Output rel=canonical
*
* @package WordPress
* @since 2.9.0
*/
function rel_canonical() {
$link = false;
if ( is_singular() ) {
$link = get_permalink( get_queried_object() );
if ( $page = get_query_var('page') && 1 != $page )
$link = user_trailingslashit( trailingslashit( $link ) . get_query_var( 'page' ) );
} else {
if ( is_front_page() ) {
$link = trailingslashit( get_bloginfo('url') );
} else if ( is_home() && get_option('show_on_front') == "page" ) {
$link = get_permalink( get_option( 'page_for_posts' ) );
} else if ( is_tax() || is_tag() || is_category() ) {
$term = get_queried_object();
$link = get_term_link( $term, $term->taxonomy );
} else if ( is_post_type_archive() ) {
$link = get_post_type_archive_link( get_post_type() );
} else if ( is_archive() ) {
if ( is_date() ) {
if ( is_day() ) {
$link = get_day_link( get_query_var('year'), get_query_var('monthnum'), get_query_var('day') );
} else if ( is_month() ) {
$link = get_month_link( get_query_var('year'), get_query_var('monthnum') );
} else if ( is_year() ) {
$link = get_year_link( get_query_var('year') );
}
}
}
if ( $link && $paged = get_query_var('paged') && $paged > 1 ) {
global $wp_rewrite;
$link = user_trailingslashit( trailingslashit( $link ) . trailingslashit( $wp_rewrite->pagination_base ) . $paged );
}
}
 
$link = apply_filters( 'rel_canonical', $link );
if ( $link )
echo "<link rel='canonical' href='$link' />\n";
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.