Echo the Canonical URL after a page’s content

Ever needed to add the Canonical URL of your page after the content? Well, here we show you how. All it takes is the Yoast WP-SEO plugin and the ability to paste a bit of code into your functions.php file.

Why would I add it to the page?

If you are syndicating to other sites as part of your content marking campaign, it’s nice to have a link back. It shows the site that you are syndicating, acknowledges their ownership, and it shows trust to your visitor allowing them to know where the article came from.

Add the following code to your functions.php

Add the following to your function.php (using a child theme is best).

function monsters_of_search_before_after($content) {
	$canonicalURL = WPSEO_Frontend::get_instance()->canonical( false );
	$posttitle = get_the_title();
    $contentbefore = '';
    $contentafter = '<span class="canoncial-url">Article Source: <a target="_blank" href="'.$canonicalURL.'" title="'.$posttitle.'">'.$canonicalURL.'</a></span>';
    $returncontent = $contentbefore . $content . $contentafter;
    return $returncontent;
add_filter('the_content', 'monsters_of_search_before_after');