Created
September 16, 2019 13:16
-
-
Save termitkin/269a19d93a16b802714ba2d30ea34892 to your computer and use it in GitHub Desktop.
[WordPress] Add link rel="canonical" to <head>
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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