Search code examples
tagscontent-management-systemorchardcmsmeta

How to add multiple metaentries in orchard?


I need to add two meta tags. I have this:

        MetaEntry FBphoto = new MetaEntry();
    FBfoto.AddAttribute("property", "og:image");
    FBfoto.AddAttribute("content", "image.jpg");
    SetMeta(FBphoto);

    MetaEntry FBdescription = new MetaEntry();
    FBdescription.AddAttribute("property", "og:description");
    FBdescription.AddAttribute("content", "Some description");
    SetMeta(FBdescription);

Unfortunatelly SetMeta adds only second metaentry (FBdescription). How can I achieve to add them both? Thank you in advance!


Solution

  • The reason this doesn't work is because if you look in ResourceManager.SetMeta there's the following code

    public void SetMeta(MetaEntry meta) {
    if (meta == null) {
        return;
    }
    
    var index = meta.Name ?? meta.HttpEquiv ?? "charset";
    
    _metas[index] = meta;
    

    }

    Therefore your second call to the method is overwriting the entry from the first (the one with the key "charset").

    If you look at a module called Social Media Tags on the Orchard Gallery and the source on GitHub they are adding the tags in the driver building up the meta in a helper class

    In that code, they are just using the key as the property name without the colon so your code would become

    MetaEntry FBphoto = new MetaEntry();
    FBphoto.Name = "ogimage";
    FBphoto.AddAttribute("property", "og:image");
    FBphoto.AddAttribute("content", "image.jpg");
    SetMeta(FBphoto);
    
    MetaEntry FBdescription = new MetaEntry();
    FBdescription.Name = "ogdescription";
    FBdescription.AddAttribute("property", "og:description");
    FBdescription.AddAttribute("content", "Some description");
    SetMeta(FBdescription);
    

    which does get both the tags on the page. I don't think there's any harm in having the name attibutes, they just aren't required, but you'd need to confirm this.