Skip to content

Cross-page admin-link clicks: state, destructive actions, referer hint - #195

Merged
AllTerrainDeveloper merged 2 commits into
trunkfrom
fix/iframe-bridge-cross-bundle-and-destructive-actions
May 13, 2026
Merged

AllTerrainDeveloper merged 2 commits into
trunkfrom
fix/iframe-bridge-cross-bundle-and-destructive-actions

Conversation

@AllTerrainDeveloper

@AllTerrainDeveloper AllTerrainDeveloper commented May 13, 2026 •

Copy link
Copy Markdown
Collaborator

What you'll notice

  • Clicking Trash / Untrash / Delete on a row in the Posts list now does what vanilla WordPress does: the same list refreshes in place with the "1 post moved to the Trash. Undo." banner. No new window. No surprise jump to the Dashboard.
  • The same goes for comment moderation row actions — Approve / Spam / Trash on the Comments screen all refresh in place.
  • Clicking Edit (or any genuine "take me to a different screen" link) still opens a new window, exactly as before.
  • A new public API lets plugin authors mark their own redirect-back action URLs as in-place: wp.desktop.registerDestructiveAdminAction(...).

Why this needs fixing

Three real bugs were stacked on top of each other. They mask each other; you have to peel them off one at a time.

1. Cross-page admin-link clicks weren't being intercepted at all

Click any in-iframe admin link that goes to a different page — say, an Edit link on a Posts row — and the click was silently swallowed. No new window. No navigation. Nothing.

The shell's window code lives in one JavaScript bundle, and the main desktop code lives in another. Both bundles import the same dispatch module — but each bundle ends up with its own private copy of that module's state. The main bundle was correctly wiring the dispatcher; the window bundle was reading its own never-populated copy and doing nothing. The fix: stash that state on a single shared object both bundles read from.

2. Trash was opening a new window… on the Dashboard

Once #1 was fixed and cross-page clicks worked again, Trash had a new problem: it opened a fresh window, and that window showed the Dashboard, not the Posts list.

Why? When you open a brand-new iframe and immediately point it at post.php?action=trash, the iframe has no prior page in its history. The browser's Referer header falls back to the embedder's URL — i.e., whichever page the desktop shell is currently displaying (often the Dashboard after login). WordPress's trash handler reads that Referer to decide where to send the user after the deletion. So the post got trashed correctly, then WordPress sent the user "back" to the Dashboard, because that's where it thought they came from.

This isn't a vanilla WordPress problem because in vanilla WP, Trash is a same-tab navigation — the Referer is the Posts list URL, exactly what WP needs.

The proper fix is to stop opening a new window for these actions entirely. In real WordPress, the Trash row click never opens a new tab — the list just refreshes in place with an Undo notice. We now do the same thing: the destructive action runs against the source iframe, so the iframe's existing URL becomes the Referer, and WP redirects right back to the list as it should.

3. The Referer header itself is unreliable

Even with #2 in place — even when the action runs against the source iframe whose URL is correct — many environments set a Referrer-Policy header that strips the path off the Referer (sending only http://your-site.com, without the /wp-admin/edit.php part). That breaks WordPress's "send me back where I came from" logic just as effectively as #2 did.

Belt-and-braces fix: we attach _wp_http_referer=<source-page> as a query parameter on the action URL. WordPress checks that parameter before falling back to the raw Referer header — it's the same hint WordPress itself uses for form posts via wp_nonce_field(). So the redirect destination is deterministic regardless of what header the browser ends up sending.

4. Plugins should be able to opt in to the same behavior

Built-in WordPress has a fixed set of redirect-back action names: trash, untrash, delete, spamcomment, etc. We hard-code those for zero-config coverage. But plenty of plugins introduce their own destructive actions (WooCommerce order trash, for example). Without an extension point, those would keep opening stray windows.

New stable API:

wp.desktop.registerDestructiveAdminAction( {
    id: 'woocommerce/trash-order',
    matches: ( _url, parsed ) =>
        parsed.pathname.endsWith( '/admin.php' ) &&
        parsed.searchParams.get( 'page' ) === 'wc-orders' &&
        parsed.searchParams.get( 'action' ) === 'trash' &&
        parsed.searchParams.has( '_wpnonce' ),
} );

Predicate-based (rather than a name list) so plugins can match on whatever combination of path / query / nonce uniquely identifies their action. Symmetric with the existing registerNativeUrlRemap so authors recognize the pattern. Returns an unregister function. Companion unregisterDestructiveAdminAction(id) and listDestructiveAdminActions() for completeness.

Files

File What changed
src/window/iframe-bridge.ts The actual dispatch fix: shared state, destructive-action short-circuit, _wp_http_referer stamping in both branches.
src/native-url-remap.ts Same shared-state fix applied to the URL remap registry, which had the same latent issue.
src/destructive-admin-actions.ts New module — the plugin-extensible registry.
src/api/facade.ts Wires the new functions onto wp.desktop.
src/desktop.ts Adds the new methods to the public API's TypeScript interface.
src/window/iframe-bridge.test.ts 9 new tests covering each new branch.
src/destructive-admin-actions.test.ts New file — 11 unit tests for the registry.
docs/javascript-reference.md Reference docs for the new API.

Test plan

  • Hard-reload wp-admin with desktop mode on.
  • Click Trash on a Posts row → same window refreshes, "1 post moved to the Trash. Undo." banner shows.
  • Click Edit on a Posts row → new window opens on the editor (unchanged behavior).
  • Open the Trash view → Restore and Delete Permanently also refresh in place.
  • On the Comments screen → Approve / Spam / Trash row actions refresh in place.
  • Bookmark / admin-bar / "Edit Post" deep links → still open a new window with the correct page.
  • No regressions on dock clicks, dashboard widgets, plugin pages opened from dock.
  • npm run test:js — 1153 tests pass.
  • npm run lint, tsc --noEmit, npm run build — clean across all bundles.

@github-actions

github-actions Bot commented May 13, 2026 •

Copy link
Copy Markdown
Contributor

✅ WordPress Plugin Check Report

✅ Status: Passed

📊 Report

All checks passed! No errors or warnings found.


🤖 Generated by WordPress Plugin Check Action • Learn more about Plugin Check

@AllTerrainDeveloper
AllTerrainDeveloper merged commit ae08a19 into trunk May 13, 2026
5 checks passed
@AllTerrainDeveloper
AllTerrainDeveloper deleted the fix/iframe-bridge-cross-bundle-and-destructive-actions branch May 13, 2026 10:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant