Using wp_enqueue_script on scripts that contain PHP

If you’re outputting the script in the footer, you can call wp_localize_script at any point after your script is enqueued but before the script is output on wp_footer priority 20. function wp_enqueue_woocommerce_style(){ wp_enqueue_script( ‘chroma_js’, get_template_directory_uri() . ‘/js/chroma.js’, ”, ‘1.1’, true ); } add_action( ‘wp_enqueue_scripts’, ‘wp_enqueue_woocommerce_style’ ); function wp_localize_woocommerce_style(){ $array = array( ‘var1’ => $var1, ‘var2’ … Read more

jQuery plugin not loading

It might be the case that you are using you JS validation before enqueuing this script. You code may be like.. <html> $(“#myForm”).validate(); or JS file that uses validate() jquery.validate.js here </html> Above code is just for demonstrating. So you are using validate() function before it has been enqueued so it is throwing validate() is … Read more

How to register script with null value for version?

You literally specify the version number as null: function add_ajax_script() { wp_register_script( ‘ajax-implementation’, get_stylesheet_directory() . ‘/scripts/ajax-implementation.js’, array(‘jquery’), null, false); wp_enqueue_script(‘ajax-implementation’); } add_action( ‘wp_enqueue_scripts’, ‘add_ajax_script’ ); An empty string, ” is not the same thing as null.

Add crossorigin to SCRIPT tag

script_loader_tag or script_loader_src filters are there to let you tweak the HTML of the script easily so you can add custom attributes: add_filter(‘script_loader_tag’, function($tag, $handle){ switch ( $handle ) { case ‘foo’: $tag = preg_replace( ‘/src=[\’|”|]/i’, ‘crossorigin $0’, $tag ); break; } return $tag; }, 10, 2); To avoid conflicting with other plugins, pass unique … Read more

How to register and set http/2 server prefetch for a specific asset?

Support for resource hints in WordPress is done through rel attribute of <link> elements whithin the HTML docuemnt, not with HTTP/2 server push (which uses Link headers in the HTTP response). You can use wp_resource_hints filter to add the URLs you need the prefetch like this: add_filter( ‘wp_resource_hints’, ‘cyb_resource_hints’, 10, 2 ); function cyb_resource_hints( $urls, … Read more

Starting with tabbed metabox

First, you have to create the metabox. Then you have to add tabs to the metabox. For the first step, to create a metabox, here is a tutorial for you. You can use that as bases for your metabox, and maybe modify it according to your needs. For the second step, you have to add … Read more

Why is WordPress enqueuing admin relevant scripts (e.g., React, ReactDOM, Redux, hooks, TinyMCE etc) when not logged in?

If your bootstrap or other enqueued scripts have a dependency or name conflict, this could enqueue all these scripts. There are a large number of common scripts in WordPress core that are enqueued under common names. I always recommend prefixing your script names with something specific. wp_enqueue_script( ‘theme-bootstrap’, ‘https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js’, array( ‘jquery’ ), ‘4.3.1’, true );