From 865039153f6891154f2dcd4b89fc15e7f95025fe Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Fri, 6 May 2022 10:12:52 -0600 Subject: [PATCH 1/4] Add a front end JQuery enqueue check --- checks/class-jquery-check.php | 55 +++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 checks/class-jquery-check.php diff --git a/checks/class-jquery-check.php b/checks/class-jquery-check.php new file mode 100644 index 00000000..6bf2c3c4 --- /dev/null +++ b/checks/class-jquery-check.php @@ -0,0 +1,55 @@ +error[] = sprintf( + '%s: %s', + __( 'RECOMMENDED', 'theme-check' ), + sprintf( + 'JQuery is enqueued on the front end, however you may not need it, see this article for tips on switching to vanilla JavaScript.', + 'https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/' + ) + ); + } + return true; + } + + /** + * Get error messages from the checks. + * + * @return array Error message. + */ + public function getError() { + return $this->error; + } +} + +$themechecks[] = new JQuery_Check(); From 21f0a9142cb145e552c0bb3863b532d4072c0650 Mon Sep 17 00:00:00 2001 From: Adam Silverstein Date: Mon, 16 May 2022 12:51:51 -0600 Subject: [PATCH 2/4] Update link, point to make post --- checks/class-jquery-check.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/class-jquery-check.php b/checks/class-jquery-check.php index 6bf2c3c4..150adb99 100644 --- a/checks/class-jquery-check.php +++ b/checks/class-jquery-check.php @@ -35,7 +35,7 @@ public function check( $php_files, $css_files, $other_files ) { __( 'RECOMMENDED', 'theme-check' ), sprintf( 'JQuery is enqueued on the front end, however you may not need it, see this article for tips on switching to vanilla JavaScript.', - 'https://tobiasahlin.com/blog/move-from-jquery-to-vanilla-javascript/' + 'https://make.wordpress.org/themes/2021/10/04/the-performance-impact-of-using-jquery-in-wordpress-themes/' ) ); } From 98f6208b195913291908eb1fb97977b69ed77d7a Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Fri, 18 Sep 2026 10:56:32 -0700 Subject: [PATCH 3/4] Fix inverted jQuery enqueue condition 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) Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR --- checks/class-jquery-check.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/checks/class-jquery-check.php b/checks/class-jquery-check.php index 150adb99..591b4428 100644 --- a/checks/class-jquery-check.php +++ b/checks/class-jquery-check.php @@ -29,7 +29,7 @@ public function check( $php_files, $css_files, $other_files ) { checkcount(); - if ( ! preg_match( '/wp_enqueue_script\(\s?("|\')jquery("|\')/i', $php ) ) { + if ( preg_match( '/wp_enqueue_script\(\s?("|\')jquery("|\')/i', $php ) ) { $this->error[] = sprintf( '%s: %s', __( 'RECOMMENDED', 'theme-check' ), From a8ce1074b6016492dc7ece76071a722ec4302be3 Mon Sep 17 00:00:00 2001 From: adamsilverstein Date: Fri, 18 Sep 2026 10:56:45 -0700 Subject: [PATCH 4/4] Detect jQuery loaded as a script dependency 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) Claude-Session: https://claude.ai/code/session_01XymJGmRuNKwf8srrtsrcLR --- checks/class-jquery-check.php | 43 +++++++++++++++++++++++++++++------ 1 file changed, 36 insertions(+), 7 deletions(-) diff --git a/checks/class-jquery-check.php b/checks/class-jquery-check.php index 591b4428..8f48133a 100644 --- a/checks/class-jquery-check.php +++ b/checks/class-jquery-check.php @@ -1,12 +1,15 @@ $file_content ) { + if ( ! preg_match( $direct_regex, $file_content ) && ! preg_match( $dependency_regex, $file_content ) ) { + continue; + } + + $grep = tc_preg( '/jquery/i', $file_path ); + $this->error[] = sprintf( - '%s: %s', + '%s: %s %s', __( 'RECOMMENDED', 'theme-check' ), sprintf( - 'JQuery is enqueued on the front end, however you may not need it, see this article for tips on switching to vanilla JavaScript.', + /* translators: 1: file name, 2: URL of an article about the performance impact of jQuery. */ + __( 'jQuery is loaded by %1$s, either enqueued directly or listed as a dependency of another script. You may not need it, see this article for tips on switching to vanilla JavaScript.', 'theme-check' ), + '' . tc_filename( $file_path ) . '', 'https://make.wordpress.org/themes/2021/10/04/the-performance-impact-of-using-jquery-in-wordpress-themes/' - ) + ), + $grep ); } + return true; }