Can the WordPress API run inside fetched posts?
-
I’m successfully fetching and displaying post content using the WordPress API:
const postContentDiv = document.getElementById('thePost'); const apiUrl = 'https://example.com/wp-json/wp/v2/posts?slug=example-post; fetch(apiUrl) .then((response) => response.json()) .then((data) => { const postContent = data[0].content.rendered; postContentDiv.innerHTML = postContent; })However, the given post contains custom HTML with JavaScript which, for our example case, is of this kind:
<script> document.addEventListener("DOMContentLoaded", function(event) { foo(); }); function foo() { return new Promise(resolve => { console.log("foo"); }) } </script>I see the
<script>tags attached to the DOM, but it’s not running. I first tried witheval()which didn’t work, then I thought the problem was thedocument.addEventListenerblock, but some attempts to remove it apparently don’t work either.Any suggestions?
The page I need help with: [log in to see the link]
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
The topic ‘Can the WordPress API run inside fetched posts?’ is closed to new replies.