Aktualisieren Sie die jquery-Version
2 Antworten
- Stimmen
-
- 2017-02-21
Warnung: Sie sollten diejQuery-Kernversionnichtersetzen,insbesondereim Admin-Bereich . Da viele WordPress-Kernfunktionen von der Version abhängen können. Ein anderes Plugin kann auch von derim Kern hinzugefügten Version
jQuery
abhängen.Wenn Sie sicher sind,dass Sie die Kernversion von
jQuery
ändernmöchten,können Siein diesem Fall denfolgenden CODEin die Dateifunctions.php
Ihres aktiven Themaseinfügen (gerade)besser,wenn Sieein Plugin dafürerstellen):function replace_core_jquery_version() { wp_deregister_script( 'jquery' ); // Change the URL if you want to load a local copy of jQuery from your own server. wp_register_script( 'jquery', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' ); } add_action( 'wp_enqueue_scripts', 'replace_core_jquery_version' );
Diesersetzt die Kernversion
jQuery
und lädt stattdessen die Version3.1.1
vom Google-Server.Obwohl nichtempfohlen ,können Sie diefolgende zusätzliche CODE-Zeile verwenden,um diejQuery-Version auchin
wp-admin
zuersetzen :add_action( 'admin_enqueue_scripts', 'replace_core_jquery_version' );
Auf diese Weise haben Sie auchnach dem Aktualisieren von WordPress diegewünschte Version von
jQuery
.Eineetwasbessere Funktion:
Die obige Funktion
replace_core_jquery_version
entfernt auch das vom WordPress-Kern hinzugefügte Skriptjquery-migrate
. Diesist sinnvoll,da dieneueste Version vonjQuerymit einer älteren Version vonjquery-migrate
nicht ordnungsgemäßfunktioniert. Sie könnenjedoch aucheine neuere Version vonjquery-migrate
hinzufügen. Verwenden Siein diesem Fall stattdessen diefolgende Funktion:function replace_core_jquery_version() { wp_deregister_script( 'jquery-core' ); wp_register_script( 'jquery-core', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' ); wp_deregister_script( 'jquery-migrate' ); wp_register_script( 'jquery-migrate', "https://code.jquery.com/jquery-migrate-3.0.0.min.js", array(), '3.0.0' ); }
Warning: You shouldn't replace core jQuery version, especially in the admin panel. Since many WordPress core functionality may depend on the version. Also, other plugin may depend on the
jQuery
version added in the core.If you are sure that you want to change the core
jQuery
version, in that case you may add the following CODE in your active theme'sfunctions.php
file (even better if you create a plugin for this):function replace_core_jquery_version() { wp_deregister_script( 'jquery' ); // Change the URL if you want to load a local copy of jQuery from your own server. wp_register_script( 'jquery', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' ); } add_action( 'wp_enqueue_scripts', 'replace_core_jquery_version' );
This will replace core
jQuery
version and instead load version3.1.1
from Google's server.Also, although not recommended, you may use the following additional line of CODE to replace the jQuery version in
wp-admin
as well:add_action( 'admin_enqueue_scripts', 'replace_core_jquery_version' );
This way, even after updating WordPress, you'll have the version of
jQuery
as you want.A slightly better function:
The
replace_core_jquery_version
function above also removesjquery-migrate
script added by WordPress core. This is reasonable, because the newest version of jQuery will not work properly with an older version ofjquery-migrate
. However, you can include a newer version ofjquery-migrate
as well. In that case, use the following function instead:function replace_core_jquery_version() { wp_deregister_script( 'jquery-core' ); wp_register_script( 'jquery-core', "https://code.jquery.com/jquery-3.1.1.min.js", array(), '3.1.1' ); wp_deregister_script( 'jquery-migrate' ); wp_register_script( 'jquery-migrate', "https://code.jquery.com/jquery-migrate-3.0.0.min.js", array(), '3.0.0' ); }
-
Können Sie die Aktioneinfach ausfunctions.phpentfernen,wenn Siefeststellen,dass sie Ihr Themabeschädigt?Wirdes auf die ursprünglichejQuery-Version zurückgesetzt oder handeltes sich umeine dauerhafte Änderung?Can you just remove the action from functions.php if you find that it breaks your theme? Will it revert back to the original jQuery version or is this a permanent change?
- 0
- 2018-08-23
- Nick
-
Wenn die Rückruffunktion der Aktion "wp_enqueue_scripts"nurjQuery aktualisiert undjQuery voneiner anderen Stellein die Warteschlangegestellt wird,wird durch Entfernen der Aktion die ursprünglichejQuery wiederhergestellt.Manchmal zwischenspeichern Browserjedoch den alten CODE,abhängig von den Server-Cache-Einstellungen. Dahermüssen Sie den Browser-Cache löschen,nachdem Sie diesgetan haben,um die Änderung zu sehen.If the callback function of the `wp_enqueue_scripts` action only updates jQuery and jQuery is being enqueued from somewhere else, then removing the action will restore original jQuery. However, sometimes browsers cache old CODE, depending on server cache settings, So, you'll need to clear browser cache after you do that to see the change.
- 1
- 2018-08-23
- Fayaz
-
ok dankeich wolltenur sicherstellen,dassichmeine Seitenicht vermasselt habe,bevorich diese Aktion hinzugefügt habe.Ich hatte das Gefühl,dass das,was Siegesagt haben,richtigist,aberich wollte auf Nummer sichergehen.ok thanks i just wanted to make sure i didn't screw up my site before adding this action. I had a feeling what you said is accurate but I wanted to play it safe.
- 0
- 2018-08-23
- Nick
-
Diesist offensichtlich **nichtpermanent **,da diese Änderung vom CODE selbst abhängt undnichtsin der Datenbankgespeichertist.Wenn Sie also den zugehörigen CODEentfernen,wird der alte Status wiederhergestellt.This is obviously **not permanent**, as this change depends on the CODE itself, nothing is saved on the database. So removing the related CODE will revert back to old the state.
- 1
- 2018-08-23
- Fayaz
-
Das Migrationsskriptfür 3.x-Versionenfunktioniertnicht,wenn Plugins/Themeseine Version unter 1.12erwarten.Mehr dazu hier: https://wordpress.stackexchange.com/a/244543/75495The migrate script for 3.x versions does not work when plugins/themes expect a version below 1.12. More about that here: https://wordpress.stackexchange.com/a/244543/75495
- 0
- 2018-10-24
- cjbj
-
@cjbj dasistnatürlich derfall.Bei der Frageging esjedoch darum,"1.12"fallen zu lassen,daher deckt die Antwort dies offensichtlichnicht ab.@cjbj of course that's the case. However, the question was about ditching `1.12`, so obviously the answer doesn't cover that.
- 0
- 2018-10-24
- Fayaz
-
Leider scheintesin WordPress 5nicht zufunktionieren. Die alte JQuery wirdentladen,aber dieneue wirdnichtgeladen.Sadly, does not appear to work in WordPress 5. The old Jquery is unloaded, but the new is not loaded.
- 0
- 2020-03-05
- TelFiRE
-
- 2019-07-01
Ich habeein Pluginfür dieses spezielle Problementwickelt. Das Plugin spieltnichtmit WordPressjQuery,daesnurim Frontendgeladen wird. Siehe: jQuery Managerfür WordPress
Warumnochein jQuery Updater/Manager/Entwickler/Debugging-Tool?
Da Siemit keinem der Entwicklertoolseine bestimmte auswählen können Version vonjQuery und/oderjQuery Migrate. Bereitstellung der Produktion und dieminimierte Version. Siehe Funktionen unten!
✅ Wirdnurim Frontend ausgeführt und stört WordPressnicht Admin/Backend und WP Customizer (aus Kompatibilitätsgründen) Siehe: https://core.trac.wordpress.org/ticket/45130 und https://core.trac.wordpress.org/ticket/37110
✅ Ein-/Ausschalten jQuery und/oderjQuery Migrate
✅ Aktivieren Sieeine spezifische Version vonjQuery und/oderjQuery Migrate
Und vielesmehr! Der Codeist Open Source,Sie können also studieren es,lernen Sie daraus undtragen Sie dazubei.
Fastjeder verwendet denfalschen Griff
WordPress verwendettatsächlich dasjquery-core-Handle,nichtjquery:
// jQuery $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4' ); $scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.12.4' ); $scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.4.1' );
Das Handle jquery istnurein Alias zum Laden von jquery-core mit jquery-migrate
Weitere Informationen zu Aliase: wp_register_scriptmehrere Bezeichner?Der richtige Weg
Inmeinem Beispiel unten verwendeich auch das offiziellejQuery-CDN unter https://code.jquery.com Verwenden Sie script_loader_tag ,damiticheinige CDN-Attribute hinzufügen kann.
Sie können denfolgenden Code verwenden:// Front-end not excuted in the wp admin and the wp customizer (for compatibility reasons) // See: https://core.trac.wordpress.org/ticket/45130 and https://core.trac.wordpress.org/ticket/37110 function wp_jquery_manager_plugin_front_end_scripts() { $wp_admin = is_admin(); $wp_customizer = is_customize_preview(); // jQuery if ( $wp_admin || $wp_customizer ) { // echo 'We are in the WP Admin or in the WP Customizer'; return; } else { // Deregister WP core jQuery, see https://github.com/Remzi1993/wp-jquery-manager/issues/2 and https://github.com/WordPress/WordPress/blob/91da29d9afaa664eb84e1261ebb916b18a362aa9/wp-includes/script-loader.php#L226 wp_deregister_script( 'jquery' ); // the jquery handle is just an alias to load jquery-core with jquery-migrate // Deregister WP jQuery wp_deregister_script( 'jquery-core' ); // Deregister WP jQuery Migrate wp_deregister_script( 'jquery-migrate' ); // Register jQuery in the head wp_register_script( 'jquery-core', 'https://code.jquery.com/jquery-3.3.1.min.js', array(), null, false ); /** * Register jquery using jquery-core as a dependency, so other scripts could use the jquery handle * see https://wordpress.stackexchange.com/questions/283828/wp-register-script-multiple-identifiers * We first register the script and afther that we enqueue it, see why: * https://wordpress.stackexchange.com/questions/82490/when-should-i-use-wp-register-script-with-wp-enqueue-script-vs-just-wp-enque * https://stackoverflow.com/questions/39653993/what-is-diffrence-between-wp-enqueue-script-and-wp-register-script */ wp_register_script( 'jquery', false, array( 'jquery-core' ), null, false ); wp_enqueue_script( 'jquery' ); } } add_action( 'wp_enqueue_scripts', 'wp_jquery_manager_plugin_front_end_scripts' ); function add_jquery_attributes( $tag, $handle ) { if ( 'jquery-core' === $handle ) { return str_replace( "type='text/javascript'", "type='text/javascript' integrity='sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=' crossorigin='anonymous'", $tag ); } return $tag; } add_filter( 'script_loader_tag', 'add_jquery_attributes', 10, 2 );
I have developed a plugin for this specific problem. The plugin doesn't mess with WordPress jQuery as it is only loaded in the front-end. See: jQuery Manager for WordPress
Why yet another jQuery Updater / Manager / Developer / Debugging tool?
Because none of the developer tools lets you select a specific version of jQuery and/or jQuery Migrate. Providing both the production and the minified version. See features below!
✅ Only executed in the front-end, doesn't interfere with WordPress admin/backend and WP customizer (for compatibility reasons) See: https://core.trac.wordpress.org/ticket/45130 and https://core.trac.wordpress.org/ticket/37110
✅ Turn on/off jQuery and/or jQuery Migrate
✅ Activate a specific version of jQuery and/or jQuery Migrate
And much more! The code is open source, so you could study it, learn from it and contribute.
Almost everybody uses the incorrect handle
WordPress actually uses the jquery-core handle, not jquery:
// jQuery $scripts->add( 'jquery', false, array( 'jquery-core', 'jquery-migrate' ), '1.12.4' ); $scripts->add( 'jquery-core', '/wp-includes/js/jquery/jquery.js', array(), '1.12.4' ); $scripts->add( 'jquery-migrate', "/wp-includes/js/jquery/jquery-migrate$suffix.js", array(), '1.4.1' );
The jquery handle is just an alias to load jquery-core with jquery-migrate
See more info about aliases: wp_register_script multiple identifiers?The correct way to do it
In my example below I use the official jQuery CDN at https://code.jquery.com I also use script_loader_tag so that I could add some CDN attributes.
You could use the following code:// Front-end not excuted in the wp admin and the wp customizer (for compatibility reasons) // See: https://core.trac.wordpress.org/ticket/45130 and https://core.trac.wordpress.org/ticket/37110 function wp_jquery_manager_plugin_front_end_scripts() { $wp_admin = is_admin(); $wp_customizer = is_customize_preview(); // jQuery if ( $wp_admin || $wp_customizer ) { // echo 'We are in the WP Admin or in the WP Customizer'; return; } else { // Deregister WP core jQuery, see https://github.com/Remzi1993/wp-jquery-manager/issues/2 and https://github.com/WordPress/WordPress/blob/91da29d9afaa664eb84e1261ebb916b18a362aa9/wp-includes/script-loader.php#L226 wp_deregister_script( 'jquery' ); // the jquery handle is just an alias to load jquery-core with jquery-migrate // Deregister WP jQuery wp_deregister_script( 'jquery-core' ); // Deregister WP jQuery Migrate wp_deregister_script( 'jquery-migrate' ); // Register jQuery in the head wp_register_script( 'jquery-core', 'https://code.jquery.com/jquery-3.3.1.min.js', array(), null, false ); /** * Register jquery using jquery-core as a dependency, so other scripts could use the jquery handle * see https://wordpress.stackexchange.com/questions/283828/wp-register-script-multiple-identifiers * We first register the script and afther that we enqueue it, see why: * https://wordpress.stackexchange.com/questions/82490/when-should-i-use-wp-register-script-with-wp-enqueue-script-vs-just-wp-enque * https://stackoverflow.com/questions/39653993/what-is-diffrence-between-wp-enqueue-script-and-wp-register-script */ wp_register_script( 'jquery', false, array( 'jquery-core' ), null, false ); wp_enqueue_script( 'jquery' ); } } add_action( 'wp_enqueue_scripts', 'wp_jquery_manager_plugin_front_end_scripts' ); function add_jquery_attributes( $tag, $handle ) { if ( 'jquery-core' === $handle ) { return str_replace( "type='text/javascript'", "type='text/javascript' integrity='sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=' crossorigin='anonymous'", $tag ); } return $tag; } add_filter( 'script_loader_tag', 'add_jquery_attributes', 10, 2 );
Ichführe WordPress Version 4.7.2 aus.undes verwendetjQuery Version 1.12.Ichmuss diese Version aufeine höhere Version aktualisieren.Ich habees zuvor durcheine neue Versionersetzt,aber wennich den WordPress-Kern aktualisiere,wirdes wieder durch 1.12ersetzt. Wie kannich die Version vonjQuery ändern,die WordPress dauerhaft verwendet?