Site icon Nabtron

Manually Canonical WordPress permalinks without plugins

First of all let me make it clear that WordPress “does” add canonical urls to your <head> section but it’s “buggy” and bad for seo. You can see that when it shows comments pages and each page, although having same article content, shows different canonical. This leads to duplicate content and/or duplicate title warning in google webmaster tools.

Now some might like the ideas as such too, but then gentleman, what’s the purpose of canonical? if it’s going to show all the pages original link as canonical. We will discuss on other post that how canonical should be, but here let’s see how to make it that way that it shows the original articles link as canonical and not the comments page’s url.

One way to achieve the good canonical urls is to use the fancy seo plugins out there like yoast and all in one seo etc, but I prefer not to use them due to their bugs and their incorrect (or different) opinion about seo at certain things. So if you’re like me who wants to code and modify their blog themselves and not just hand them over to fancy plugins, this page is for you.

Manual code for Canonical WordPress permalinks

WordPress uses rel_canonical function to display the canonical tag in your wp blogs source code. We can’t simply use add_filter to override the function inside another function (wp_head in this case) as discussed in the post: WP function add_filter not working.

So we’re going to adopt another strategy, we will remove the rel_canonical function and then add our custom function with same code but modified to meet our needs to show the proper seo optimized canonical urls.

You need to goto your current theme > functions.php file and on top of the file add the following code (after <?php ) . . . I hope you know basic programming though! 0_o. First lets see the two parts of the code that we will use and then simply copy the complete code from below and paste it in your functions.php file. Don’t forget to backup your theme files before editing!

Remove rel_canonical function

The following code in functions.php will remove the already existing rel_canonical function

if( function_exists( 'rel_canonical' ) )
{
    remove_action( 'wp_head', 'rel_canonical' );
}

Add custom rel_canonical function for optimized seo canonical urls

Following the code above, use the following code to show our optimized canonical urls: (note it’s edited out of original rel_canonical function of WordPress itself but optimized for seo for google specially)

The comment (by “//”) is the part commented by us to fulfill what we wanted out of seo optimized canonical urls.

add_action( 'wp_head', 'rel_canonical_nabtron' );
function rel_canonical_nabtron() {

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 );

echo "<link rel='canonical' href='$link' />\n";
}

Complete code to have custom good canonical urls for your wordpress manually

if( function_exists( 'rel_canonical' ) )
{
remove_action( 'wp_head', 'rel_canonical' );
}

add_action( 'wp_head', 'rel_canonical_nabtron' );

function rel_canonical_nabtron() {

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' />\n";

}

This will help you fix the google webmaster tools errors of duplicate content and or duplicate title tags and will improve your blogs seo.

Please let me know if you had any issues while replacing your wp blogs default “bad” canonical urls with seo optimized canonical urls or if you need my assistance to do it on your website.

Exit mobile version