Skip to content

Administration: Skip loading assets for hidden dashboard widgets - #12831

Draft
sanketio wants to merge 1 commit into
WordPress:trunkfrom
sanketio:feat/55344
Draft

Administration: Skip loading assets for hidden dashboard widgets#12831
sanketio wants to merge 1 commit into
WordPress:trunkfrom
sanketio:feat/55344

Conversation

@sanketio

@sanketio sanketio commented Aug 4, 2026

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/55344

Summary

Dashboard widgets hidden via Screen Options still enqueue their scripts and
styles, wasting resources on every dashboard load. This adds an optional
$enqueue_callback parameter to wp_add_dashboard_widget() so a widget's
assets are only enqueued when the widget is actually visible.

This is a proof-of-concept for Approach A proposed on the ticket (comment #13).

What changed

  • wp_add_dashboard_widget() gains an optional 8th parameter,
    $enqueue_callback (default null, fully backward-compatible).
  • When provided, core reads get_hidden_meta_boxes() once and only runs the
    callback when the widget is not hidden.
  • Collapsed (closed) widgets are treated as visible, since they remain in the DOM.

Backward compatibility

  • Default null → existing 7-argument callers are untouched.
  • First visit (no stored preference) → all widgets visible, assets load as today.
  • Un-hiding a widget without a page reload will lack its JS until the next full
    load — the same trade-off as every other meta-box screen, accepted in comment Remove post_ID requirement from parse_embed AJAX call #7.

Usage

add_action( 'wp_dashboard_setup', function () {
    wp_add_dashboard_widget(
        'my_widget',
        'My Widget',
        'my_widget_render',
        null, null, 'normal', 'core',
        function () {
            wp_enqueue_script( 'my-widget' );
        }
    );
} );

How to test

  1. Register a dashboard widget using the new $enqueue_callback to enqueue a script.
  2. Load the dashboard → the script loads.
  3. Hide the widget via Screen Options, reload → the script is no longer enqueued.
  4. Collapse (not hide) the widget, reload → the script still loads.

Tests

Adds five PHPUnit tests in tests/phpunit/tests/admin/includesTemplate.php
covering visible, hidden, collapsed-not-hidden, null-callback (BC), and
first-visit cases. All pass; PHPCS and PHPStan clean.

@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

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