We recently required the use of canonicals on a client’s site. This meant that we had to come up with an easy solution for us to be able to assign individual canonical URLs on a page by page basis. As our client’s site was using Coaster CMS the solution was rather simple, and as there are likely to be other users of Coaster CMS out there, who may require the same functionality, we thought we’d write a blog about it.
Creating a block for canonical URLs
To get started you will need to create a new link block. This can have any name you like, although something like ‘canonical’ will do fine. After this block has been created you will need to assign it either theme-wide within the ‘theme_blocks’ table or to an individual template within the ‘template_blocks’ table. By default Coaster comes with a SEO content category that usually goes by the ID of ‘2’. For ease of use we opted to assign the block to the SEO category.
Editing the header
After the canonical block has been created, you will need to make a code changes to the header section of your theme:
<?php $canonical = PageBuilder::block(‘canonical’); ?>
@if ( !empty($canonical) && $canonical != ‘block not found’ )
<link rel=”canonical” href=”//www.electrofreeze.co.uk{{ PageBuilder::block(‘canonical’) }}” />
@endif
Place the code above just before the close of the <head> tag and save the file. This code simply checks to see whether the block is empty or whether the content of the block isn’t equal to ‘block not found’. Now you can seamlessly manage canonicals from right within Coaster.