Skip to content

Instantly share code, notes, and snippets.

@termitkin
Created September 16, 2019 13:16
Show Gist options
  • Save termitkin/269a19d93a16b802714ba2d30ea34892 to your computer and use it in GitHub Desktop.
Save termitkin/269a19d93a16b802714ba2d30ea34892 to your computer and use it in GitHub Desktop.
[WordPress] Add link rel="canonical" to <head>
if (function_exists('rel_canonical')) {
remove_action('wp_head', 'rel_canonical');
}
function add_rel_canonical() {
if (!is_singular())
return;
global $wp_the_query;
if (!$id = $wp_the_query->get_queried_object_id())
return;
$link = get_permalink($id);
echo ('<link rel="canonical" href="'. $link . '" />');
}
add_action('wp_head', 'add_rel_canonical');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment