You are viewing an old revision of this post, from April 10, 2018 @ 00:43:42. See below for differences between this version and the current revision.

Enqueue different and/or multiple versions of jQuery in WordPress

Adapted from past work and this article on WP Engine's blog. [php]add_action( 'wp_enqueue_scripts', 'pp_custom_scripts_enqueue' ); function pp_custom_scripts_enqueue() { $theme = wp_get_theme(); // Get the current theme info for version numbers // The default WordPress version of jQuery is left to enqueue as intended. // Register and inline a second specific version of jQuery wp_register_script( 'jquery3.2.1', 'https://code.jquery.com/jquery-3.2.1.min.js' ); wp_add_inline_script( 'jquery3.2.1', 'var jQuery3_2_1 = $.noConflict(true);' ); // Register custom scripts with dependency on the default WordPress jQuery version wp_register_script( 'js_main', get_stylesheet_directory_uri().'/scripts/js_min.js', array( 'jquery' ), $theme['Version'], true); // Register a script with dependencies and version in the footer // Register custom scripts with dependency on the specific version of jQuery we enqueued wp_register_script('js_main_custom_version', get_stylesheet_directory_uri().'/scripts/js_min_custom_version.js', array('jquery3.2.1'), $theme['Version'], true); // Register a script with dependencies and version in the footer // Enqueue registered scripts wp_enqueue_script('js_main'); wp_enqueue_script('js_main_custom_version'); // Localize scripts $localize_var_args = array( 'site_title' => get_bloginfo('name'), 'home_url' => home_url(), 'theme_url' => get_stylesheet_directory_uri(), 'ajaxurl' => admin_url( 'admin-ajax.php' ) ); wp_localize_script( 'js_main', 'wp_js_vars', $localize_var_args ); }[/php]

Revisions

Revision Differences

April 10, 2018 @ 00:43:42Current Revision
Content
Unchanged: Adapted from past work and this article on <a href="https:/ /wpengine.com.au/support/ including-a-different-jquery- version-in-wordpress/">WP Engine's blog</a>.Unchanged: Adapted from past work and this article on <a href="https:/ /wpengine.com.au/support/ including-a-different-jquery- version-in-wordpress/">WP Engine's blog</a>.
Deleted: [php]&lt;/pre&gt; 
Deleted: &lt;div&gt;add_action( 'wp_enqueue_scripts', 'pp_custom_scripts_enqueue' );&lt;/div&gt; Added: [php]add_action( 'wp_enqueue_scripts', 'pp_custom_scripts_enqueue' );
Deleted: &lt;div&gt;function pp_custom_scripts_enqueue() {&lt;/div&gt; Added: function pp_custom_scripts_enqueue() {
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;$theme = wp_get_theme(); // Get the current theme info for version numbers&lt;/div&gt; Added: $theme = wp_get_theme(); // Get the current theme info for version numbers
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// The default WordPress version of jQuery is left to enqueue as intended.&lt;/div&gt; Added: // The default WordPress version of jQuery is left to enqueue as intended.
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// Register and inline a second specific version of jQuery&lt;/div&gt; Added: // Register and inline a second specific version of jQuery
Deleted: &lt;div&gt;wp_ register_script( 'jquery3.2.1', 'https://code.jquery.com/ jquery-3.2.1.min.js' );&lt;/div&gt; Added: wp_register_script( 'jquery3.2.1', 'https://code.jquery.com/ jquery-3.2.1.min.js' );
Deleted: &lt;div&gt;wp_ add_inline_script( 'jquery3.2.1', 'var jQuery3_2_1 = $.noConflict(true);' );&lt;/div&gt; Added: wp_add_inline_script( 'jquery3.2.1', 'var jQuery3_2_1 = $.noConflict(true);' );
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// Register custom scripts with dependency on the default WordPress jQuery version&lt;/div&gt; Added: // Register custom scripts with dependency on the default WordPress jQuery version
Deleted: &lt;div&gt;wp_ register_script( 'js_main', get_stylesheet_ directory_uri( ).'/scripts/js_min.js', array( 'jquery' ), $theme['Version'], true); // Register a script with dependencies and version in the footer&lt;/div&gt; Added: wp_register_script( 'js_main', get_stylesheet_ directory_uri( ).'/scripts/js_min.js', array( 'jquery' ), $theme['Version'], true); // Register a script with dependencies and version in the footer
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// Register custom scripts with dependency on the specific version of jQuery we enqueued&lt;/div&gt; Added: // Register custom scripts with dependency on the specific version of jQuery we enqueued
Deleted: &lt;div&gt;wp_ register_script('js_main_ custom_version', get_stylesheet_ directory_uri( ).'/scripts/ js_min_custom_ version.js', array('jquery3.2.1'), $theme['Version'], true); // Register a script with dependencies and version in the footer&lt;/div&gt; Added: wp_register_script( 'js_main_custom_version', get_stylesheet_ directory_uri( ).'/scripts/ js_min_custom_ version.js', array('jquery3.2.1'), $theme['Version'], true); // Register a script with dependencies and version in the footer
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// Enqueue registered scripts&lt;/div&gt; Added: // Enqueue registered scripts
Deleted: &lt;div&gt;wp_ enqueue_script('js_main') ;&lt;/div&gt;Added: wp_enqueue_script('js_main');
Deleted: &lt;div&gt;wp_ enqueue_script( 'js_main_custom_ version');&lt;/div&gt;Added: wp_enqueue_script( 'js_main_custom_version');
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;// Localize scripts&lt;/div&gt; Added: // Localize scripts
Deleted: &lt;div&gt;$localize_var_args = array(&lt;/div&gt; Added: $localize_var_args = array(
Deleted: &lt;div&gt;'site_title' =&gt; get_bloginfo( 'name'),&lt;/div&gt; Added: 'site_title' =&gt; get_bloginfo('name'),
Deleted: &lt;div&gt;'home_url' =&gt; home_url(),&lt;/div&gt; Added: 'home_url' =&gt; home_url(),
Deleted: &lt;div&gt;'theme_url' =&gt; get_stylesheet_ directory_uri( ),&lt;/div&gt; Added: 'theme_url' =&gt; get_stylesheet_ directory_uri(),
Deleted: &lt;div&gt;'ajaxurl' =&gt; admin_url( 'admin-ajax.php' )&lt;/div&gt; Added: 'ajaxurl' =&gt; admin_url( 'admin-ajax.php' )
Deleted: &lt;div&gt;); &lt;/div&gt;Added: );
Deleted: &lt;div&gt;wp_ localize_script( 'js_main', 'wp_js_vars', $localize_var_args );&lt;/div&gt; Added: wp_localize_script( 'js_main', 'wp_js_vars', $localize_var_args );
Deleted: &lt;div&gt;&lt;/div&gt; 
Deleted: &lt;div&gt;}&lt;/div&gt; 
Deleted: &lt;pre&gt;[/php]Added: }[/php]

Note: Spaces may be added to comparison text to allow better line wrapping.

Tags: ,

No comments yet.

Leave a Reply