Embedding Discourse Comments via Javascript

Discourse has the ability to embed the comments from a topic in a remote site using a Javascript API that creates an IFRAME. For an example of this in action, check out Coding Horror’s blog. The blog is run via Ghost but the comments are embedded from his Discourse forum.

One important thing to note with this setup is that users have to navigate to your forum to post replies. This is intentional, as we feel that the posting interface on a Discourse forum is currently much richer than what we could embed via Javascript.

This guide will show how to set up comment embedding on your own blog or web site.

How it works

In Discourse, a topic is made up of many posts. When you are embedding Discourse on another site, you are linking a document (blog entry, HTML page, etc.) with a single topic. When people post in that topic, their comments will automatically show up on the page you embedded it in.

You have the choice to have Discourse create the topics automatically when a new embedding is found, or you can create the topics yourself in advance.

Configuring Discourse for Embedding (simple setup)

The following setup will embed a comment feed on a page on a fake blog URL of http://example.com/blog/entry-123.html, from a discourse forum running at =DISCOURSE=.

Domain for  

Domain for  

  1. Visit Admin > Customize > Embedding on your Discourse install. https://=DISCOURSE=/admin/customize/embedding

  2. Create at least one embeddable host. This should be the hostname (domain) where you want to embed your comments. In this case the host is =BLOG= – note the lack of http:// and path.

    • Class Name is a class name that will be attached every time your iframe is rendered. You can use it for styling purposes.

    • Path Whitelist allows you to specify the paths on the remote host that would accept your embed.

    • Post to Category - if you supply a category alongside the host you are entering, posts imported from that host will automatically end up in that category. Different hosts can post to different categories.

  3. Fill in the name of a user on your discourse who will create topics in the Embed by Username field. Let’s assume our discourse has a user called eviltrout, so the value is eviltrout.

  4. Insert the following HTML on the web page at http://=BLOG=/blog/entry-123.html

<div id='discourse-comments'></div>

<script type="text/javascript">
  window.DiscourseEmbed = { discourseUrl: 'https://=DISCOURSE=/',
                     discourseEmbedUrl: 'http://=BLOG=/blog/entry-123.html' };

  (function() {
    var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
    d.src = window.DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
  })();
</script>

The configurable parts of the snippet are in the DiscourseEmbed object. discourseUrl is the full path to the base of your discourse, including the trailing slash. The discourseEmbedUrl is the document that is currently embedding a comment feed.

If you set this up correctly, the first time you visit http://=BLOG=/blog/entry-123.html it will try to load comments for the blog post. Since there are none, it will tell the Discourse forum to create a new topic in the background. A new topic will be created by eviltrout and the contents of the first post will be crawled from your blog and the text will be extracted automatically.

Once the new topic is created, users can post on it, and their comments will automatically be displayed the next time http://=BLOG=/blog/entry-123.html is visited.

Embedding on more than one page

In the above example we hard coded our http://=BLOG=/blog/entry-123.html URL when embedding the snippet of Javascript. This usually won’t be enough as many sites have many pages that are generated automatically. For example on a blog each entry typically gets its own page. To support this, put the same snippet on each page you want to display comments on, but replace the value passed to discourseEmbedUrl with the current page’s URL. On my blog, I use the following value for discourseEmbedUrl: 'http://eviltrout.com<%= current_page.url %>' – as new blog pages are created, new topics will be created for them automatically on Discourse.

Styling your Embedded content

If you are running Discourse 1.4.0 or greater, you have the ability to add a stylesheet for your embedded comments. Use the Embedded CSS section of the Theme editor at Admin > Customize > Themes > Edit CSS/HTML and you can add a custom stylesheet that will be served up with your embedded comments. By default we think the layout looks nice on a white background, but if your site has a unique layout you’ll want to style it yourself.

(Optional) Adding a Feed for Polling

As mentioned above, Discourse will automatically crawl any site it is embedded on. However, sometimes HTML can be hard to parse and it might not extract the contents of your posts correctly. Many blogs and web sites support RSS/Atom feeds for syndication, and Discourse can use this to extract the content of your blog posts more accurately.

If you have a RSS or Atom feed set up on the site you are embedding Discourse into, you can configure Discourse to extract the content of posts from there by installing the Discourse RSS Polling plugin and adding your feed’s URL to the plugin’s settings. Once the feed URL has been added, Discourse will parse the feed and publish its posts to the appropriate category based on the Allowed Hosts that you add to your Embedding settings.

(Alternate Configuration) Linking to existing topics

Some people prefer to not have Discourse create topics for them automatically on their forums. They’d like to create the topics themselves, then simply tell their embedding code what topic they want to associate with. You can do this by slightly changing your embedding code:

<div id='discourse-comments'></div>

<script type="text/javascript">
  window.DiscourseEmbed = { discourseUrl: 'https://=DISCOURSE=/',
                     topicId: 12345 };

  (function() {
    var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
    d.src = window.DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
    (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);
  })();
</script>

The only difference here is we’ve replaced discourseEmbedUrl with the id of a topic from Discourse. If you do this, no topic will be created and the comments from that topic will automatically be displayed.

Setting the referrer policy

Due to recent (September 2020) changes to the default referrer policy that is set by many browsers, Discourse now explicitly defaults to setting the iframe’s referrer policy to "no-referrer-when-downgrade". If, for security reasons your site requires a stricter referrer policy, it can be set by adding a discourseReferrerPolicy value to the embed script’s DiscourseEmbed object. For example:

DiscourseEmbed = { discourseUrl: 'https://forum.example.com/',
                                  discourseEmbedUrl: '<your_posts_canonical_URL>',
                                  discourseReferrerPolicy: 'strict-origin-when-cross-origin'};

Embedding comments from a private site

For private Discourse instances, if Discourse is on a subdomain of the blog’s domain, comments will be displayed for users who are logged into Discourse. Users who are not logged into Discourse will see a ‘refused to connect’ message. If Discourse and the blog are on completely separate domains, no comments will be displayed for private forums.

Troubleshooting

The most common issue users have when embedding Discourse is setting the correct value for the embeddable hosts you added. Make sure to double check that it is only the domain of your site, and contains no extra slashes or invalid characters.

85 Likes
Embedding forum on custom site
Deeply integrating Discourse into website
Discourse 1.4 Released!
Is it possible to test Discourse Embed comment on localhost?
Trouble connecting drupal and discourse
Learning Management Systems
Create topic when a link is clicked
Using Discourse in a web app
Can be integrated with liquid templates?
Embedded Comments iframe Height
Discourse blog comments like your blog comments
Comment Reply Threading / Linking on the WordPress Side
Is it possible to use Discourse as a "commenting system"?
Imported topic via embedding will not render normally
Using Discourse to power comments in an event system or blog?
Initialising a discourse forum on a Ghost Blog
What software you use for blog.discourse.org?
Integrating Discourse with a larger social network app?
Use Discourse as the comments section of a Jekyll site?
Using Discourse as a comment system
Handling large pictures of embedded comments inside jquery window
Auto-post To Discourse Forum When Post On Another Blog?
"Error Embedding" comments
Embedded comments not displayed due to X-Frame-Options DENY
What is Discourse Blog built with?
Failed to execute postMessage on DOMWindow: The target origin provided does not match
Embedding Discourse сomments with GitBook
"Error Embedding" comments
Shared Discourse/Static Site Login
Trying to do embedding. Hit with “The referer did not match any of the following hosts:” issue
Looking for Ruby-based CMS recommendations as a compliment to Discourse
[Paid] Embedding: Allowing one RSS feed and one username per host
Discourse comment-like discussion
Discourse embedding error: Failed to execute postMessage on DOMWindow
Anybody using Discourse for Blog Comments on a Gatsby site?
Discourse + Ghost integration and SEO
Display all posts in embedding topics with +100 comments
Grav integration
Embedding Topic topic-map or Topic Summary on Other Site
Embedded topic comments are missing if containing page's URL includes a fragment identifier
Wordpress integration by iFrame instead of sync
Not generate topic automatically for all blog posts
Using Discourse as commenting system via JavaScript: Delay load or load on scroll
Link to Discourse topic without displaying Comments (in Drupal)
Which CMS for Discourse
Understanding how embedding works on remote site
Arguments against integrated intranet solutions?
Embed single discourse Topics to articles on news website
Displaying comments embed as a link without using the WP plugin
Failure embedding some topics
Connect Joomla to the discourse
Get right title embedding comments
Disqus-like commenting on Wordpress
News Plugin :newspaper:
How to integrate in discourse in zenodo
Configure the Discourse RSS Polling Plugin
Getting Discourse URL on embedded comments
Integrating registered users notifications+avatar into static website
Pandoc integration
How to configure the allowed embed selectors setting
How can I get the list of Discourse Topic IDs dynamically
Threaded comments when using Discourse for website comments?
No `Referer:`, no embedding
Commenting system: integrate Discourse and Webflow
WP Discourse Embed Plugin
Embedding error
[Question/Request] Featured image url for post
Can i use / embed discourse to make comments on pages hosted for example on Aquia
Emoji are huge when embedding
Set topic title with Discourse Embed
Embedding Discourse Profile via Javascript
WP-discourse: comments are not pulled over
discourseEmbed, does not match recipient window origin
How to embed whole forum in another site
Possible to add comment count (to a Ghost blog)?
How can I delete or undelete topics?
Link existing forum topic to new WordPress post
How to enable Discourse comments via JavaScript in Wordpress?
How to link the existing discourse comments to wordpress posts?
Embed whole forum
WP Discourse Embed Plugin
WP Discourse Embed Plugin
Synchronising log out of discourse when logging out of WordPress
Topic as widget
Discourse a a comment system with React Native and Meteor
A post's TopicEmbed isn't destroyed when the post is deleted
Pages from Google search are not rendering
Controlling amount of embedded text
Zendesk Article Comment in Discourse Integration
Flood of new topics from Embedded Discourse
Discourse topics as Dokuwiki talk pages
Multiple cors origins on hosted discourse?
WordPress / Discourse API Strategy: Get Reply Count via API Efficiently
Error Embedding for existing posts
Making embedded JavaScript based discourse commenting responsive
[paid] Preference to create & embed a topic via JavaScript only when a user clicks the "Start Discussion" link
Embedding: Discussion not showing up for new posts
Allowing WP comments while showing Discourse comments too

Is it possible to use this method if the Discourse category is hidden from public view?

Probably not, but it might work if all of your viewers are logged in.

1 Like

I’ve created a tutorial that walks you through the process of setting up a local discourse instance on vagrant for your local tests to embed discourse in your front-end development project. You can find it here: Local discourse: vagrant, ansible, lxd, docker, discourse-embedding

This is different from other descriptions that I found before in that it allows you to run locally a complete discourse set-up like you would do in production to get a feel for the “real” set-up process. In addition this set-up is able to serve as a local development playground for embedding discourse into your project.

1 Like

Hi, is there a way to disable embedding for posts in draft status? I can’t see any handlebar within Ghost for post status, can I change the embed settings to exclude /p/ which is used to preview posts? I have a problem where posts that are previewed are being added to Discourse. Cheers.

Can’t seem to find the setting to style the embedding iframe any more…? Is the original post out of date somehow?

EDIT: Nevermind, it had just changed places. I can add it to a theme component :slight_smile: thanks!

1 Like

This sounds like a ghost question, not a Discourse question? However your Ghost integration is working needs to take that into account, not Discourse.

3 Likes

Hello everyone!
I need some help to embed Discourse into READTHEDOCS (using Sphinx)
Does anyone have this kind of experience?

Looks like there is https://github.com/pdavide/sphinxcontrib-discourse which always embed a single topic.

4 Likes

Thank you for reply, let check it out

Hey Guys
I’ve been having some trouble embedding to a specific topic:
I’ve been trying different variations of settings and I still just get ‘Error Embedding’ On My Website.
Is it noticeable at all what I may be doing wrong? I’ve spent forever trying to get it to work, any help is appreciated.

Current Code Embedded On Website

Are there any errors in your Javascript console?

In your first screenshot, you have all the paths set to publish to the uncategorized category. If you want to publish all posts to the same category you only need to create one Embeddable host record.

The Path Whitelist values that you have added to your Host entries might be causing problems. You can leave that value blank to publish all posts from your blog site that you’ve added the Discourse embed script to. If you want to set a specific path to publish to a Discourse category, the path should end with /.*. For example /sites/.*

The double slash at the start of your /t/newsletter-discussions/105 path may be a problem. Also, that looks like the path to a Discourse topic. If set, the path should be the path to a grouping of posts on your blog. The purpose of the Path Whitelist setting is to allow you to publish posts from a specific path on your blog to a specific Discourse category.

3 Likes

Ok thank you Simon, I appreciate it. I will give this a shot.

Like @codinghorror, I also use this DIscourse integration with Ghost using the stock configuration which uses the absolute URL as the discourseEmbedURL.

A week or so ago, I decided to change my Ghost blog’s routing so that posts with URLs like https://engineerworkshop.com/2020/02/20/how-to-set-up-wireguard-on-a-raspberry-pi/ are redirected to https://engineerworkshop.com/blog/how-to-set-up-wireguard-on-a-raspberry-pi/, with https://engineerworkshop.com/blog/how-to-set-up-wireguard-on-a-raspberry-pi/ being the new permalink.

The Problem

You can probably anticipate the problem this creates: the discourseEmbedURL is set to the old URL and so when the embed script is called, which now has a different URL, a new comment post is created in Discourse.

Solution Attempt (Which Failed)

Now, prior to this, I had anticipated this problem, and so I thought I was being clever using a regex pattern with remap to fix the URLs: rake posts:remap["[0-9]{4}\/[0-9]{2}\/[0-9]{2}","blog",regex]

While that did “sort of” work, wherever the URL was used in the original Discourse comment post, sadly, when I loaded the blog posts, new comment topics were created in Discourse.

Ultimately, I just reloaded every post on my site and transferred comments from the old topics to my new topics, deleting the originals (I know, I know). Obviously killing pages is bad for SEO, not to mention end-user experience.

My Question

I assume the reason my regex patch attempt didn’t work is because the Discourse topic is somehow keyed to the URL on the backend. (I readily admit to being ignorant of Discourse’s data structure). For future knowledge, and anyone else that may attempt to do the same in the future, is there a way to remap a DIscourse topic to a new blog post URL?

Thanks in advance!

4 Likes

7 posts were merged into an existing topic: WP Discourse Embed Plugin