Trustpilot Widgets not rendering on mobile and modals

If you’re using Trustpilot widgets on a WordPress website (especially with Divi, Popups for Divi, or dynamic elements like content toggles), you might notice that the widgets sometimes fail to render — especially on mobile devices, inside modals, or after dynamic content loads.

Instead of the full Trustpilot widget, you might only see a fallback inline link like this:

trustpilot widget fallback not rendering

Here’s what’s really going on — and how to fix it.

Why Trustpilot Widgets Fail to Load

Trustpilot widgets are rendered by a script that looks for elements with the .trustpilot-widget class. However, if those elements are added to the DOM after the script runs, they will not render unless the script is manually triggered again.

This often happens:

  • When content is injected dynamically (e.g., modals, toggles, tabs)
  • On slow or mobile networks
  • When using asynchronous or deferred loading for Trustpilot

The Fix: Reactivate Trustpilot After DOM Changes

By using a MutationObserver, you can watch for changes in the DOM and manually call Trustpilot.loadFromElement() whenever necessary. Here’s a robust and efficient way to do it:

  • Detects when new .trustpilot-widget elements are inserted
  • Automatically re-renders them using Trustpilot’s API
  • Prevents infinite loops by temporarily disabling the observer during updates
JavaScript
<script>
(function () {
  let observer = null;
  let debounceTimer = null;

  function reloadTrustpilotWidgets() {
    if (typeof Trustpilot !== "undefined" && typeof Trustpilot.loadFromElement === "function") {
      console.log("[Trustpilot] Reload triggered.");

      // Temporarily disconnect the observer to avoid infinite mutation loops
      if (observer) observer.disconnect();

      document.querySelectorAll(".trustpilot-widget").forEach((widget, index) => {
        console.log(`[Trustpilot] Reloading widget #${index}`, widget);
        Trustpilot.loadFromElement(widget, true);
      });

      // Reattach the observer after a short delay
      setTimeout(() => {
        observer && observer.observe(document.body, {
          childList: true,
          subtree: true
        });
        console.log("[Trustpilot] Observer re-attached.");
      }, 1000);
    } else {
      console.warn("[Trustpilot] Script not available or not yet initialized.");
    }
  }

  function debounceReload(reason = "") {
    console.log(`[Trustpilot] Debounce triggered (${reason})`);
    clearTimeout(debounceTimer);
    debounceTimer = setTimeout(() => {
      reloadTrustpilotWidgets();
    }, 300);
  }

  document.addEventListener("DOMContentLoaded", () => {
    console.log("[Trustpilot] DOM ready. Triggering initial load.");
    setTimeout(() => reloadTrustpilotWidgets(), 500);

    observer = new MutationObserver((mutationsList) => {
      let foundTrustpilotChange = false;

      for (const mutation of mutationsList) {
        if (
          mutation.addedNodes &&
          Array.from(mutation.addedNodes).some(node =>
            node.nodeType === 1 &&
            (node.classList.contains("trustpilot-widget") || node.querySelector?.(".trustpilot-widget"))
          )
        ) {
          foundTrustpilotChange = true;
          break;
        }
      }

      if (foundTrustpilotChange) {
        debounceReload("mutation observed");
      }
    });

    observer.observe(document.body, {
      childList: true,
      subtree: true
    });

    console.log("[Trustpilot] MutationObserver initialized.");
  });

  if ("requestIdleCallback" in window) {
    requestIdleCallback(() => {
      console.log("[Trustpilot] Idle fallback triggered.");
      reloadTrustpilotWidgets();
    });
  }
})();
</script>

Where to Add It?

We recommend placing this code in your sitewide footer, especially if you use WPCode, Divi Theme Options, or custom JS integrations.


Final Result

With this fix:

  • Trustpilot widgets will render properly inside modals, toggles, popups, and on mobile.
  • No more fallback text or flickering.
  • Works seamlessly with Divi, Popups for Divi, and Divi Supreme toggles.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.