Deprecation of  wc_enqueue_js()  in 10.4

Starting with WooCommerce 10.4, the  wc_enqueue_js()  function is deprecated and scheduled for removal in a future version. Extension and theme authors should migrate to the recommended WordPress core script patterns as described below.

Why is  wc_enqueue_js()  Being Deprecated?

The deprecation follows community requests and aligns WooCommerce with modern WordPress practices. The  wc_enqueue_js()  function always injected inline JS code within a jQuery wrapper, even when the code did not require jQuery. This resulted in unnecessary reliance on jQuery and added extra JavaScript overhead on sites that may not otherwise load it, impacting site performance and resource usage.

How can developers tell if they are affected?

Search for usage of wc_enqueue_js() within source code.

What action do developers need to take if affected?

Update your code to follow WordPress scripted best practices:

  • Register your script with wp_register_script(), explicitly specifying dependencies as needed.
  • Enqueue your script with wp_enqueue_script().
  • Add inline JavaScript using wp_add_inline_script(), referencing the registered script handle and only declaring dependencies that are actually required[1].

Example Migration

Old pattern (deprecated):

wc_enqueue_js( "console.log('Hello from WooCommerce!');" );

New recommended pattern:

$handle = 'your-custom-script';
wp_register_script( $handle, '', array(), false, true );
wp_enqueue_script( $handle );
wp_add_inline_script( $handle, "console.log('Hello from WooCommerce!');" );

If your inline script does not need jQuery, do not declare jquery as a dependency.

Additional Information


Leave a Reply

Your email address will not be published. Required fields are marked *