我已经为这个特定的问题开发了一个插件。这个插件不会影响WordPress jQuery,因为它只在前端加载。请参见:jQuery Manager for WordPress
为什么还有另一个jQuery更新程序/管理器/开发人员/调试工具
因为没有任何开发人员工具允许您选择jQuery和/或jQuery迁移的特定版本。提供生产版和缩小版。请参见下面的功能!
✅ 仅在前端执行,doesn\'t interfere with WordPress
admin/backend and WP customizer (出于兼容性原因)请参阅:https://core.trac.wordpress.org/ticket/45130 和https://core.trac.wordpress.org/ticket/37110
✅ Turn on/off jQuery和/或jQuery迁移
✅ 激活aspecific version jQuery和/或jQuery迁移的
还有更多!代码是开源的,因此您可以研究它,从中学习并做出贡献。
几乎每个人都使用不正确的句柄,WordPress实际上使用的是jquery核心句柄,而不是jquery:
https://github.com/WordPress/WordPress/blob/91da29d9afaa664eb84e1261ebb916b18a362aa9/wp-includes/script-loader.php#L226
// 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 句柄只是要加载的别名
jquery-core 具有
jquery-migrate查看有关别名的更多信息:
wp_register_script multiple identifiers?正确的方法在下面的示例中,我使用官方jQuery CDNhttps://code.jquery.com 我还使用script_loader_tag 这样我就可以添加一些CDN属性
您可以使用以下代码:
// 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 );