Skip to main content
Stack Overflow
  1. About
  2. For Teams

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

GitHub Gist embedded in a WordPress post fetched through WP REST API

After adding an embedded Gist in my WP post, all I see when fetching it through REST API is the embed code, meaning <script src="https://gist.github.com/username/b5f6f2d0xxxxxxxdf9c90cbede0e.js"></script>.

There are solutions to properly render an embedded Gist in WordPress but these solutions work on a WordPress site, not on a non-WP site merely fetching posts from WP.

Any idea how to solve this issue?

EDIT: As per this suggestion, I tried to extract the script tag from the post and re-inject it into the DOM. But, it still doesn't work. Actually even when simply loading my Gist script into a variable then injecting the content into the DOM, it doesn't work. Yet it works with any other script, for example this:

const tag = document.createElement('script');
tag.src = 'https://my-site.com/test.js';
document.querySelector('body').prepend(tag);

But the exact same snippet using instead my Gist URL fails to execute.

Why don't Gist scripts execute when injected into the DOM?

Answer*

Draft saved
Draft discarded
Cancel
4
  • Thanks, I understand what you say about extracting the script tag and then re-injecting it into the DOM. However, once reinjected in the DOM won't it fail to be executed as well, meaning simply appear in the DOM as "<script>...</script>" without actually being executed? Commented Jan 3, 2021 at 12:20
  • @drake035 I was under the impression you were making the api calls from the server, not the client, so thought that you could extract and embed it on your pages from the server, and then simply serve the page when the client requests it. If you need to do it from the client after page load, there seem to be a number of solutions such as : stackoverflow.com/questions/19737031/… Commented Jan 3, 2021 at 12:35
  • Thanks. Just used stackoverflow.com/a/19737116/871404 (createElement, appendChild...) to insert my Gist into the DOM. I now see the script tag exactly where I inserted it. But... It doesn't execute, it just there doing nothing. Am I missing something? Commented Jan 3, 2021 at 16:19
  • @drake035 without seeing your code I can’t say. I suggest you create a new post describing your effort to add the script after page load, as well as a minimal amount of code to reproduce your problem. If you do so, I’m sure several people will be able to chime in since that is a more common problem. As I’m falling asleep and typing from my phone, I won’t be able to respond for a while 😴💤 Commented Jan 3, 2021 at 16:28

default

AltStyle によって変換されたページ (->オリジナル) /