Keeping audio playing across page navigations is not something WordPress does out of the box. Pages are served and loaded the regular way, so any sound on the current page dies the moment the next one starts loading. A public radio group running multiple podcast-focused WordPress sites wanted exactly this: a pinned player that keeps a podcast running while a visitor browses the rest of the site.

Why a page reload kills the audio

Standard WordPress sites perform full page loads, without custom link interception or history manipulation. Any <audio> element on the page is destroyed and rebuilt on each navigation, which ends playback.

Two well-known escapes exist. The heavyweight option is converting the site into a headless WordPress setup with a Single Page Application front end. That is workable, but hard to justify solely for one pinned player. The lighter option is preventing the reload altogether.

The DIY interception approach

The minimal implementation is a short chain of steps:

  1. Intercept clicks on internal links.
  2. Fetch the new page's content via Ajax.
  3. Swap the retrieved content into the current page.

A quick jQuery sketch of this is close to functional on its own:

$("a").on("click", () => {
  const url = $(this).attr("href");
  $.get(url + " main", (data) => {
    $("main").html(data);
    history.pushState({}, "", url);
  });
});

Handling the back button requires listening for a popstate event, which adds only a few more lines. Structurally, the site is arranged so that the swappable region sits inside <main>, while the audio player lives outside it:

<html>
<!-- ... -->

<body>
   <main></main>
   <audio src="" controls ...></audio>
</body>
</html>

With that layout, the <main> content and the URL change, but the <audio> element is left untouched and keeps playing. A second layer of JavaScript would then need to let visitors change which podcast is playing.

Edge cases that pile up

The three-step approach raises secondary problems quickly. Does inline script inside fetched content execute? Does the <title> get updated? The number of such edge concerns grows to the point where handling them by hand becomes irritating.

Reaching for Turbo

A WordPress theme was assembled to test this pattern using Turbo rather than hand-rolled JavaScript. Turbo, the successor to Turbolinks, targets exactly this use case: it is a drop-in JavaScript library with no build step and no configuration that intercepts internal link clicks and Ajax-fetches new content. Its relevant feature here is data-turbo-permanent: an element carrying that attribute persists across the reload cycle. In the theme, the audio player in header.php is marked accordingly.

Current state of the theme

The theme is not finished. It almost works, with one glaring bug: audio stops on the first navigation, then behaves correctly on subsequent ones. That is presumed fixable, but the project was set aside and the code left public for anyone who finds it useful.

  • Live demo: pinned-audio.flywheelsites.com
  • Repository: github.com/chriscoyier/PinnedAudio

Caveats around the dependency

Turbo comes from Basecamp, and Basecamp's recent implosion makes relying on its software uncomfortable. The concern is amplified because Sam Stephenson wrote roughly 75% of Turbo and has stated he will not touch it or related projects unless the software moves to its own foundation. Turbo was already on uncertain footing, having appeared buggier than Turbolinks, so its long-term support looks shaky.