Easy meta tags for Umbraco sites

Meta keywords and meta description tags are easy to add to an Umbraco site. Simply add a Textstring and a Textbox multiple to a document type and output them in <meta> tags on the site, and you're good to go.

What's difficult about meta tags is the amount of work it requires of website editors to give them meaningful content. They might not be all that important for search engines any more, but everyone still agrees that well-structured metadata tags belong in a semantically marked up web document. So we need to help out the editors.

A website is a hierarchy of pages, and it is a reasonable assumption that a good description of any given page will also be a serviceable description of any of its subpages. A good <meta name='description'/> on the Projects page could also serve as a description on any page underneath it, describing a specific project. So what we need is to create our <meta> tags in such a way that if they are not defined for a given page, they will be inherited from the nearest ancestor in the page hiearchy that has such tags defined.

Thankfully this is trivial with the help of XSLT and XPath. Within the macro that renders the <head> content for you Umbraco site, add the following tags:

<meta name="keywords" content="{$currentPage/ancestor-or-self::* [metaKeywords != ''][1]/metaKeywords}"/>
<meta name="description" content="{$currentPage/ancestor-or-self::* [metaDescription != ''][1]/metaDescription}"/>

Provided that your document properties have the aliases metaKeywords and metaDescription, respectively, this will search upwards through the page hierarchy to find the nearest ancestor with content for each of these fields and use that content for the current page. If content is defined for the current page, that will of course be used.

Naturally, it is still preferable to enter accurate keywords and descriptive text for every singe page on a site, but when that is not feasible, this method is an easy way to ensure usefully descriptive <meta> tags on every page.