Get canonical link

Since WP 4.6 there has been a core function for this: wp_get_canonical_url()

Call it with a post ID to get that post’s canonical URL, or in the loop without a post ID to get the current post’s canonical URL.