Skip to content

Instantly share code, notes, and snippets.

@developdaly
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save developdaly/98a389b2e0cdb3945734 to your computer and use it in GitHub Desktop.
Save developdaly/98a389b2e0cdb3945734 to your computer and use it in GitHub Desktop.
This code will force HTTPS in the canonical URL meta tag of a WordPress site in an instance where SSL is not the forced protocol.
<?php
add_action( 'after_setup_theme', 'my_theme_setup' );
function my_theme_setup() {
/* Customize the canonical URL. */
remove_filter( 'wp_head', 'rel_canonical' );
add_filter( 'wp_head', 'my_rel_canonical' );
}
/**
* Output rel=canonical for singular queries.
*/
function my_rel_canonical() {
if ( !is_singular() )
return;
global $wp_the_query;
if ( !$id = $wp_the_query->get_queried_object_id() )
return;
$link = get_permalink( $id );
if ( $page = get_query_var('cpage') )
$link = get_comments_pagenum_link( $page );
$link = str_replace( 'http://', 'https://', $link );
echo "<link rel='canonical' href='" . esc_url( $link ) . "' />\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment