Embedding Discourse Comments via Javascript


(Thebravoman) #157

Had some problems with the “in a frame because it set ‘X-Frame-Options’ to ‘SAMEORIGIN’.”

I had to change the domain from example.com to www.example.com -> including the www. infront.
Only after that it started working.

(Jeff Atwood) #158

24 posts were split to a new topic: Embed Discourse as comments with on-page commenting?

Embed Discourse comments with on-page commenting?
(Lucas) #167

@eviltrout is possible to embed nested replies to a comment? Screen Shot 2016-12-29 at 23.14.04 - Jumpshare. Thanks in advance.

(Robin Ward) #168

Unfortunately no, there is no way to do that.

(Orion Burt) #169

Is there any way to open the reply editor on page load after coming from the “Continue Discussion” link in the embed or more generally via a URL/GET/POST parameter that can be added to the topic/post URL?

I understand the hesitation towards enabling too many comment system bad habits, but if this is at all possible I think it would help solve a lot of people’s issues with using Discourse for commenting without compromising the separate forum community that you are trying to encourage in the first place.

  • Grey reply links could be appended to each post in the embed that could link to Discourse with the “Reply to Post” editor open and scrolled to the corresponding post.
  • The blue Continue Discussion link could link to Discourse with the “Reply to Topic” editor open and scrolled to the last post.
  • If necessary, this could be an “enable at your own risk” feature that you have to opt in to in the Discourse Customize > Embedding settings.
  • If user is not logged in already on Discourse, then you simply skip the open the reply editor step (but keep it scrolled to the corresponding or last post).

This could also bolster the response notification emails. As it currently stands (or last I received one) the options are “Visit Topic or reply to this email to respond”. If the reply to post editor could be opened on page load, you could expand this to “Visit Topic, Reply at Forum, or reply to this email to respond” which people would appreciate if they are planning on using the very nice Discourse reply editor anyway.

(Davidweisss) #170

Can there be a wildcard to match several subfolders?

Use case:

I would like all users to be aware of the discussion about, and be able to comment on, this great new feature.

Can there be something like

Thanks would be really really cool.

(Robin Ward) #171

If you’re talking about Discourse on a subfolder, we only allow you to use one subfolder per install. (I mention this because the post you reply to was talking about that case.)

If you’re talking about embedding for multiple folders on the site you’re embedding it on, sure. You can use a regular expression in the path whitelist. For example /blog/.*

(Davidweisss) #172


Was indeed talking about embedding, sorry for the confusion.

Will try it out.

(Mudedla Panduranga Rao ) #173

Nice feature. I’m having problem when embedding discourse comments when using discourseEmbedUrl, but this is working fine when i embedded with topic id. I’m getting ‘Loading Discourse’ when i embedded with discourseEmbedUrl. Some one please help me to resolve this.

(Joe Buhlig) #174

I just ran into what I think is a bug with this. I switched my blog over to https from http. So technically speaking, Discourse sees these as new posts. Which means I’m now being inundated with new posts that are duplicates.

Is it possible to prevent this and have it check for either protocol before creating new?

Embed creating duplicate http / https topics
(Robin Ward) #175

This has come up before a couple of times (including @codinghorror’s forum) and I don’t think it’s been successfully fixed. We should probably investigate into a way of having it consider both URLs the same.

In the meantime, the solution is unfortunately to update your topic_embed table manually.

(Cassie S) #176

Hello! I actually have 2 things.

First, I can get the plugin to work on a static site (on github pages), if I use the TopicId option. However, only the comments are loading. The initial topic post is not. Is this by design, or is there a bug here?

Second, I am having problems using the standard url to create a new topic, like @mrrao. I am working on a local host, though, and some of the comments above implied that might be part of the problem? Is there any way for someone to confirm this?

(EDIT: we now have the url option working. It was apparently an issue with the configuration of our local network. It is suffering from the same problem as the TopicID, however (the initial post in the thread is not shown)


(Jeff Atwood) #177

This is correct, the design assumes it is a blog and the first post is a duplicate (excerpt, technically) of the blog body. See my blog at https://blog.codinghorror.com for an example.

(Leandro) #181

How are the topics and blogs/urls linked?
Does discourse store the discourseEmbedUrl somewhere and uses only that value (literal) to fetch the topic?

(Robin Ward) #182

The embedUrl is passed to discourse, which then enqueues a job that will crawl and import the content of the page into a topic. Comments from that topic are then displayed via an iframe on the source page.

(Leandro) #183

Is it possible to embed a topic in a protected url?
To achieve this I’ve created those topics using a public atom feed (so discourse does not need to crawl and import the content). However; when I open any url it hangs on “Loading discussion…”. I was hoping that discourse would recognise the url and load the topic anyway (without crawling).
I know this is not the expected use case, but I was wondering if it possible.


We have embedding working well, with one minor issue… Our forum is hosted in a subdirectory (sitename.com/forums). When viewing the embedded topic on the main site, the avatar images are not showing up (the urls do not include the /forums/ in the path). Oddly enough, all of the other urls in the embed are correct (the user details link, etc.)

When viewing the topic on the actual forum, the urls for the images DO correctly include the subdirectory.

I’m not seeing anything in the embed settings that would help. Any ideas?


(Robin Ward) #186

I couldn’t reproduce this. When I try locally with a relative_root set up for discourse, the embedded comments have the correct avatars, and I confirmed they have the correct path.

I reviewed the code path too and don’t see how the subdirectory could be missing - maybe it’s a plugin or something?


Thanks for taking a look. I’ll dig in a bit more and see what I can find.

(Christoph) #188

I just tried inserting the following javascript code into one of my wordpress pages by using the Code Snippets plugin:

<script type="text/javascript">
DiscourseEmbed = { discourseUrl: 'https://forum.mydomain.net/',
                               topicId: 33 };
 (function() {
   var d = document.createElement('script'); d.type = 'text/javascript'; d.async = true;
   d.src = DiscourseEmbed.discourseUrl + 'javascripts/embed.js';
   (document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(d);

and it went horribly wrong. It brought down my entire Wordpress site. I’m not kidding. Every single URL just produced a 404 error as soon as I had saved and activated the above code in the Code Snippets plugin. I hadn’t even placed it on any page yet.

So I guess you shouldn’t do that…

If the same happened to anyone else, here’s how I got WP going again: the trick is that the Code Snippets plugin has a safe-mode switch that you can add to any URL and it will deactivate all codesnippets while calling that page: it’s simply ?snippets-safe-mode=true That way I was able to navigate to the snippet and deactivate it in safe mode.

How to enable Discourse comments via JavaScript in Wordpress?