How can I automatically embed a Bandcamp player on my site?

If I post a Bandcamp URL to a discourse forum, it pulls in their Embedded player. How can I achieve this with my own music site?

I’ve tried setting og:video tags to no avail.

Thanks!

You mean in your site that isn’t discourse?

You can look in the discourse source and search for “onebox” or maybe “one_box”

Or maybe see https://get.bandcamp.help/hc/en-us/articles/360013561254-How-do-I-create-a-Bandcamp-embedded-player-#:~:text=To%20create%20an%20embedded%20player,your%20merchandise%2C%20and%20so%20on. And use that to develop code for your site.

2 Likes

Hi Jay,

I have a website, a bit similar to Bandcamp. I have created embeds similar to the link you have pasted so you can add an iframe to another website and my embed will appear.

But what I’m taking about is some magic that seems to be happening when you simply post a bandcamp URL, a full iframe embed appears. Is this something configured on the discourse server?

Let’s see if this works here. I’ll post paste a bandcamp url below.

Yes ^ as you can see an functional embedded music player has appeared… I didnt paste any iframe or js, just the embed url I quoted above.

Is this something special that discourse knows how to do for specific sites or it there some magic header tags you can set to tell it which embed url to use.

As Jay pointed out you should look at the source code.

‘Onebox’ is the library that, in part, converts some URLs into embeds.

Have you looked at?:

I think the original intention was that this code be also used on non-Discourse sites.

To that end there’s a (now unmaintained) gem that you might be able to leverage (assuming your site is RoR), maybe you’d be interested in maintaining it?

A word of caution: the code on Discourse will have positively evolved since.

3 Likes

Hi Robert,

Thank you for digging that out for me. So yes, there there is specific code for it in Discourse. It’s not something I need to do on my site as such. That answers that part. So if I want my site supported in the same way as Bandcamp on Discourse (and potentially other forums), I’ll need to ask or contribute the code to make it work for mine too.

My site’s not RoR but I do know some ruby so I might be able to contribute.


I have a 2nd question too. As well as pasting the URL, I also attempted to add the embed with the iframe tag. This works fine on a normal website but doing it on my discourse forum doesn’t work. A square box appeared but not the embed inside. I presumed it was a permission issue so I added my host to the allowed iframes but it still doesn’t work.

OK so just as I was about to get a screenshot to show it not working it seems to have started working! So I wonder if there is some kind of delay in the system for applying the allowed hosts.

I’ll explore further. Thanks for your help so far.

1 Like

Ok I’ve just realised what’s happening. When you change the allow iframe settings, it’s doesn’t effect existing posts (at least not immediately). It’s possible they do later…not sure.

But - when you create a new post or edit existing post, the settings take effect then.

So now both my embed and URL work - so this means I’ll just need to contact the relevant forums and ask them to add my domain name to the allowed hosts and it should work from that point on.

Cheers

Hi :wave: :slight_smile:

Older posts need to be rebaked. You can do manually that by clicking the three dots icon below the post, then the wrench, then Rebuild HTML.
image

If you have a lot of posts that need to be rebaked, you may need the rake task to rebake all posts or use a custom rails script to rebake just some of them using the needed criteria.

Something like this I think:

Post.where('raw LIKE ?', 'a specific URL').find_each do |p|
    p.rebake!
end
1 Like