Skip to content

Add a jQuery usage check - #423

Open
adamsilverstein wants to merge 10 commits into
WordPress:trunkfrom
adamsilverstein:add/jquery-check
Open

adamsilverstein wants to merge 10 commits into
WordPress:trunkfrom
adamsilverstein:add/jquery-check

Conversation

@adamsilverstein

@adamsilverstein adamsilverstein commented May 6, 2022 •

Copy link
Copy Markdown
Member

Addresses #402

Claude Code wrote up where the check stands after the review rounds:

Adds a RECOMMENDED notice when a theme loads jQuery on the front end, with a link to https://make.wordpress.org/themes/2021/10/04/the-performance-impact-of-using-jquery-in-wordpress-themes/ for tips on moving to vanilla JavaScript. The notice never fails a theme.

What is detected

  • A direct enqueue: wp_enqueue_script( 'jquery' ).
  • jQuery in the dependency list of an enqueued script: wp_enqueue_script( 'theme', $src, array( 'jquery' ) ). Both array() and [] are read, in any position and across lines.
  • A script registered with a jQuery dependency and then enqueued by handle, in the same file or another one, or used as a dependency of an enqueued script.
  • Any of the jQuery handles WordPress registers: jquery, jquery-core, jquery-migrate, jquery-ui-*, jquery-effects-*, jquery-masonry, jquery-color, jquery-form, jquery-hotkeys, jquery-table-hotkeys, jquery-touch-punch, jquery-query and jquery-serialize-object. A theme's own handle such as jquery-slider is not matched.

What is left out on purpose

  • wp_register_script() on its own. Registering does not load anything.
  • Calls inside a function, method or closure hooked only to admin_enqueue_scripts, customize_controls_enqueue_scripts, customize_preview_init, enqueue_block_editor_assets or login_enqueue_scripts.

The theme gets one notice, with the matching calls listed under each file name.

Known limits

  • A dependency list built in a variable first ($deps = array( 'jquery' );, or $asset['dependencies'] from a build's .asset.php file) is not followed.
  • Core scripts that depend on jQuery without having it in their name (wp-util, backbone, thickbox, mediaelement) are not counted.
  • A script registered in PHP and loaded only through block.json is not seen as enqueued.
  • Admin callbacks are matched by name, so a method name that is also hooked to a front end action elsewhere in the theme is still reported. An enqueue() method on a WP_Customize_Control subclass is reported too, since nothing hooks it.

How this was tested

The check was run outside WordPress with stubs for the Theme Check helpers, feeding it files through tc_strip_comments() the same way run_themechecks_against_theme() does. 25 small cases cover the list above (direct, dependency, register only, register then enqueue across files, commented-out calls, admin hooks and closures, dynamic handles, array access, dependency cycles).

It was also run over the bundled themes in wordpress-develop:

  • Twenty Eleven through Twenty Seventeen are reported, including jquery-masonry in Twenty Thirteen.
  • Twenty Nineteen, Twenty Twenty and the block themes are clean.
  • Twenty Twenty-One still gets one line, for the enqueue() method of its Customizer color control.

It has not been run inside a WordPress install since the rewrite, so how the grouped notice renders on the Theme Check screen still needs a look. composer standards:check passes.

AI Use

The code after the first two commits and this description came out of a few rounds with 🤖 Claude Code. I will review and test.

Comment thread checks/class-jquery-check.php Resolved
Comment thread checks/class-jquery-check.php Resolved
@adamsilverstein
adamsilverstein requested a review from aristath May 23, 2022 14:07
Comment thread checks/class-jquery-check.php Resolved
adamsilverstein and others added 3 commits September 18, 2026 10:54
Bring the four-year-old branch up to date with trunk before addressing
review feedback, so the new check is written against current conventions.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
The check was copied from the comment-reply check, where the notice fires
when the script is missing. Here the opposite is true: the notice should
fire when jQuery is found, so drop the negation. As written the check
warned every theme that does not use jQuery and stayed silent for the
themes it is meant to flag.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
Most themes never enqueue jQuery directly; they list it in the $deps
array of their own script, which loads jQuery all the same. Matching only
wp_enqueue_script( 'jquery' ) therefore missed the common case.

Add a second pattern for the dependency array of wp_enqueue_script() and
wp_register_script(), covering both array() and short array syntax, any
position in the list, and arbitrary whitespace. Both patterns accept the
jQuery handles WordPress registers - jquery, jquery-core, jquery-migrate
and the jquery-ui-* family - since each of them either is jQuery or
depends on it.

Report per file so the notice names the file and shows the matching
lines, as the other checks do, and correct the copy: jQuery is spelled
with a lowercase j, the wording now covers the dependency case, and the
lead uses tc-recommended to match its RECOMMENDED label.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR
@jamesbregenzer

Copy link
Copy Markdown

A six-case focused check of JQuery_Check at PR head a8ce1074b6016492dc7ece76071a722ec4302be3 (base 9c21ede71a1cd09aa2605f64da85e2f83822cf9d) produced two false-positive warnings:

  • wp_register_script( 'theme', 'theme.js', array( 'jquery' ) ); warns even though it registers the script without enqueueing it. In the cited WordPress Core snapshot, wp_register_script() calls $wp_scripts->add(); enqueueing is a separate operation.
  • // wp_enqueue_script( 'jquery' ); also warns although the call is commented out.

The other four cases behaved as expected: no jQuery and jquery-slider produced no warning, while direct enqueue and dependency enqueue produced warnings. The harness used the PR's checker with minimal Theme Check stubs under PHP 8.4. It did not run a full WordPress site or test rendered front-end loading, and it does not address the separate admin/front-end distinction.

Could the check distinguish registration from enqueueing and ignore commented-out calls?

@adamsilverstein

Copy link
Copy Markdown
Member Author

Could the check distinguish registration from enqueueing and ignore commented-out calls?

Good suggestion, I'll give it a try @jamesbregenzer

wp_register_script() makes a script available without loading it, so a
registration that lists jQuery as a dependency does not put jQuery on the
page by itself. Reporting it warned themes that register a script and
never use it.

Collect every wp_enqueue_script() and wp_register_script() call across
the theme, follow the dependencies of the enqueued scripts, and report a
registration only when its handle is reached that way - enqueued by
handle, in the same file or another one, or listed as a dependency of an
enqueued script.

Build the reported lines from the matched calls instead of grepping the
file for "jquery". The grep read the file from disk, so it also listed
commented-out calls and unrelated lines that merely mention jQuery next
to a real match. Multi-line calls are shown collapsed onto one line.

Match the argument list up to the parenthesis that closes the call, so
an array later in the same statement or template is no longer read as
the dependency list, and skip array access such as $urls['jquery'].

Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
jquery-masonry, jquery-color, jquery-form, the jquery-effects-* family
and the other jquery-prefixed scripts in core all depend on jQuery, so
enqueueing one loads it. Twenty Thirteen and Twenty Fourteen enqueue
jquery-masonry by handle, which the check missed.

List the handles explicitly so a theme's own jquery-* handle is still
not mistaken for a core one.

Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
The check is about the front end, yet a script enqueued for the admin,
the Customizer, the block editor or the login screen was reported like
any other. Twenty Twenty-One loads no jQuery on the front end and still
got three notices, all for Customizer scripts.

Leave out calls made inside a function, method or closure that is hooked
only to admin_enqueue_scripts, customize_controls_enqueue_scripts,
customize_preview_init, enqueue_block_editor_assets or
login_enqueue_scripts. Methods are matched by name, so a name that is
also hooked to a front end action is still reported.

Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
One notice per file repeated the whole sentence and the article link for
every file that loads jQuery. Emit one notice for the theme and list the
calls under each file name instead.

Reword it to say what was found and that the theme may not need it, as
issue 402 asked for a notice that reads as an invitation, not a warning.

Claude-Session: https://claude.ai/code/session_018vwYjVmRVUpGNNkapNPb8Y
@adamsilverstein adamsilverstein changed the title Add a front end JQuery enqueue check Add a jQuery usage check Nov 10, 2026
@adamsilverstein

Copy link
Copy Markdown
Member Author

@jamesbregenzer thanks for the focused testing, I pushed an update for the registration case.

I had Claude work through both cases, here is what changed:

wp_register_script( 'theme', 'theme.js', array( 'jquery' ) ) on its own no longer warns. The check now collects every wp_enqueue_script() and wp_register_script() call in the theme and follows the dependencies of the enqueued ones, so a registration is only reported when its handle is actually enqueued (in the same file or another one) or is a dependency of an enqueued script. Dropping wp_register_script() altogether would have missed the common register-then-enqueue pattern.

The commented-out call is a bit different. In the plugin, run_themechecks_against_theme() passes every PHP file through tc_strip_comments() before the checks run (

$php[ $filename ] = tc_strip_comments( $php[ $filename ] );
), so // wp_enqueue_script( 'jquery' ); never reaches the check. A harness that hands the raw file contents to check() skips that step, which is likely where the warning came from.

There was a related bug in the output though: when a file had a real match, the lines shown under the notice came from grepping the file on disk for "jquery", so commented-out calls and unrelated lines showed up there. The lines are now built from the matched calls only.

On the admin/front end distinction: calls inside a function, method or closure hooked only to admin_enqueue_scripts, the Customizer hooks, enqueue_block_editor_assets or login_enqueue_scripts are now skipped. The PR description lists what is still not covered.

Can you run your six cases again with comments stripped first and see if they all pass?

@jamesbregenzer

Copy link
Copy Markdown

Thanks for the update. Six reconstructed cases were rerun in an isolated PHP fixture against dd2da9d, with each PHP sample passed through the real tc_strip_comments() first. All six matched expectations: no jQuery, a theme-owned jquery-slider, registration alone, and a commented-out enqueue produced no notice; direct enqueue and dependency enqueue each produced one.

You were right about the commented-out case. The earlier harness passed raw PHP directly to check(), bypassing the plugin's comment-stripping path. The earlier report described a registration-only warning at a8ce1074; this rerun found none at the current head.

A WordPress 7.1.4 smoke test then ran the unchanged plugin through its nonce-protected admin form on two small classic test themes. The control theme produced no jQuery notice. The positive theme produced one grouped RECOMMENDED notice showing functions.php and inc/dependencies.php, each with the matching call on line 3; a commented-out distractor was absent. This checks the normal scan and generated admin HTML under PHP 8.4.27 WebAssembly with SQLite. It does not verify visual layout or focus behavior in a browser, or a native PHP/MySQL deployment.

@adamsilverstein

Copy link
Copy Markdown
Member Author

Thanks @jamesbregenzer - let me know if any additional changes are needed here.

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.

3 participants