Set meta tags from plugin?



I’m attempting to dynamically inject some meta tags into my page from a plugin, the problem right now is that by using javascript to accomplish this is that the javascript runs after the page is rendered meaning the original rendered page does not contain the meta tags and the scraper I’m targeting cannot pick up those tags as they don’t yet exist.

Is there a way to define meta tags in my specific plugin and have it add it to the page before it’s rendered, thanks.

(Mittineague) #2

Have you tried adding the meta tags using the Admin Customize UI?


Well that does appear to solve the problem in some way, however it removes any ability to make those programmatically generated so for example if I have two different configuration values that i want defined depending on the environment i’m in for example making this solution less than ideal.

(Mittineague) #4

Yes, if you want different meta tags for different pages, a solution will be more involved.

I’m unclear what you mean by “environments”, desktop vs. mobile?
What meta tag are you wanting to add?


Just using the facebook sdk as an example here but there is an app_id meta tag that can change between development and production environments, the ability to have those tags set dynamically would be ideal.

(Mittineague) #6

I see nothing that would be useful for this in the page head, but this appears near the bottom

  Discourse.CDN = '';
  Discourse.BaseUrl = ''.replace(/:[\d]*$/,"");
  Discourse.BaseUri = '';
  Discourse.Environment = 'production';
  Discourse.SiteSettings = PreloadStore.get('siteSettings');
  Discourse.LetterAvatarVersion = '5_410b5db071eaaad4484aa17e7909345f';
  I18n.defaultLocale = 'en';
  PreloadStore.get("customEmoji").forEach(function(emoji) {
    Discourse.Dialect.registerEmoji(, emoji.url);
  Discourse.Session.currentProp("disableCustomCSS", false);
  Discourse.HighlightJSPath = "/highlight-js/";
      Discourse.S3CDN = '';
    Discourse.S3BaseUrl = '//';

Have you already tried using Discourse.Environment ?


Yes that was my first attempt but this can’t be done using javascript as the javascript doesn’t run until after the page has initially been rendered, what I need is something either in the plugin.rb file or the settings.yml file that will parse the values before the page is initially rendered.

(system) #8

(Sam Saffron) #9

This is now possible, example in discourse solved.

See this example:

You register crawler custom HTML with: server:before-head-close-crawler

register_html_builder('server:before-head-close-crawler') do |controller|
    "<meta something='something'>".html_safe

You register non crawler with 'server:before-head-close :

register_html_builder('server:before-head-close') do |controller|
    "<meta something='something'>".html_safe