开发者

Stop Wordpress from automatically inserting scripts

开发者 https://www.devze.com 2023-01-14 06:15 出处:网络
Is there a way to stop Wordpress from automatically inserting scripts into my theme from wp-includes/js? It\'s kind of annoying that I can use/choose to add my own.

Is there a way to stop Wordpress from automatically inserting scripts into my theme from wp-includes/js? It's kind of annoying that I can use/choose to add my own.

Thanks!


Actually it's not including the jquery but scriptaculous.js and effects.js and for some reason they're interfering with jquery. Would it be the same just putting:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'scriptaculous' );
    wp_deregist开发者_JAVA技巧er_script( 'effects' );
}

Thanks!


Most scripts (i.e. jQuery) are queued up by plug-ins that depend on them. Since jQuery ships with WordPress, it makes it really easy to distribute a lean, lightweight plug-in and just call the script from wp-includes/js using wp_enqueue_script('jquery'). In fact, this is the recommended way to include scripts on your site (not sure how you're using/choosing your own, but that's a secondary point).

To remove default scripts, you can use a similar call to: wp_deregister_script('jquery');. This will remove the script named "jquery" from the queue and it will not be included. Here's the full code you'd place in your functions.php file:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'jquery' );
}

Keep in mind, though, that some plug-ins might still depend on this script. When you register a script, you typically add any dependencies right in the registration ... so if a script depends on jQuery and you've removed it like this ... then you'll end up breaking the plug-in because other scripts won't be included on the page (even if you manually added jQuery through a <script></script> tag on the site.

To avoid this, you'll need to use WordPress' registration/enqueuing engine to re-add your own version of jQuery:

add_action( 'wp_print_scripts', 'my_deregister_javascript', 100 );

function my_deregister_javascript() {
    wp_deregister_script( 'jquery' );
    wp_enqueue_script( 'jquery', PATH_TO_YOUR_JQUERY_VERSION, '', '1.4.2');
}

This will re-add jQuery (I assume you're using version 1.4.2, but replace the version number there with whatever you're using) and queue it back up in the system. Then any dependencies should be satisfied and everything should work as expected.

0

精彩评论

暂无评论...
验证码 换一张
取 消