Discourse Plugin: Remove Canonical Links

Is this the most simple, but useful, Discourse plugin in the world?

Here is a simple Discourse plugin I created today for two reasons:

  1. Remove all canonical links from Discourse
  2. Demonstrate overriding Ruby modules in the Discourse app

I like this plugin because it demonstrated how simple it is to override a Ruby module and as a new Discourse plugin developer, I'm trying to learn the basic concepts, one-step-a-time by creating some server-side Discourse utilities I need.

Why do I need to remove canonical links?

Currently, and for the foreseeable future, we plan to maintain our original forums as a reference site. However, we have imported the posts from the reference site to our Discourse forum. We still generate ad revenue from the reference site and the SEO is much better (on the reference site) as well.

By default, Discourse adds canonical links to every topic; but we do not want this functionality at this time. So I thought to myself, "can I easily override a Discourse module to disable this canonical link?" The answer turned out to be yes, and this is how easy it is:

cat plugin.rb

after_initialize do
  module CanonicalURL::Helpers
     def canonical_link_tag(url = nil)
        ""
     end
  end
end

Frankly, I am still learning Ruby and this is my first plugin which overrides Ruby module. I hope other beginning Discourse plugin writers will find this useful.

Future Plans

  1. Add code to check to see if the topic was imported from reference site, and if so, override the module and if not, let the canonical link process as normal (or something similar, TBD).

  2. Maybe re-add the plugin enable / disable setting. I removed this setting, as I do not plan to turn this on and off; and wanted to keep this plugin as simple as possible.

2 Likes