Override Meta Tags in Orchard CMS

Override Meta Tags in Orchard CMS

Easily override meta tags displayed by Orchard CMS by overriding the Metas Shape. ResourceManager in Orchard CMS keeps track of the meta tags in your website and displays them using the Metas Shape. You can override this shape in your Orchard Theme to remove certain meta tags, add meta tags, and filter meta tags. Although you will have to write a bit of Razor Code in your Orchard View, it is far simpler than creating an Orchard Module or modifying the Orchard Source Code.

Metas Shape in Orchard CMS

In my example I will override the Metas Shape to remove the Orchard Generator Meta Tag. By default, all Orchard Websites include a generator tag mentioning the website was created by Orchard.

<meta content="Orchard" name="generator" />

In a few occassions I have been asked how one might remove this tag for security purposes. Although I am unaware of any security flaws in Orchard CMS, one may not want to advertise the website was built by Orchard ( or any CMS ) in case a flaw is detected and malicious hackers and bots use the tag to identify it as an Orchard Website. Personally I leave the meta tag, but it's a valid point.

The easiest way I know to do this without a lot of coding is to override the Metas Shape used to display the Orchard Meta Tags. I am sure there are other ways, and possibly better ways, but this method is simple and effective for such an easy scenario.

In the Orchard Theme one can simply override the Metas Shape by creating a Metas.cshtml file and add a bit of code that gets the meta tags from ResourceManager and filters the Orchard Generator Meta Tag. Here is some sample code.

@using Orchard.UI.Resources
@{
    var resourceManager = WorkContext.Resolve<IResourceManager>();
    var metas = resourceManager
        .GetRegisteredMetas()
        .Where(meta => string.IsNullOrWhiteSpace(meta.Name)
           || !meta.Name.Equals("generator"));

    foreach(var meta in metas) {
        <text>@Html.Raw(meta.GetTag())</text>
    }
}

This code essentially does the same thing as the original Metas Shape with the exception for the filtering of the Orchard Generator Meta Tag.

I created a quick git respository for the example, called OrchardMetas.

Conclusion

That's just a quick tip for Orchard Web Developers who are interested in doing a bit of shape overriding to manipulate the Orchard Meta Tags. It's a simple technique that requires less ceremony and sophistication to do a bit of customization of Orchard Meta Tags. Check out my other Orchard Tutorials.

Follow me on Google+ and Twitter!