3

i need to exclude certain scripts from running on a certain page. i have tried this code but it's not working - when i inspect the page the scripts are still being loaded into the footer of my page.

here is my code in functions.php

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

function my_deregister_javascript() {
   if ( !is_page('my-page') ) {
    wp_deregister_script( 'isotope' );
    wp_deregister_script( 'anystretch' );
    wp_deregister_script( 'imgliquid' );
    wp_deregister_script( 'retina' );
    wp_deregister_script( 'menutron' );
    wp_deregister_script( 'imagesloaded' );
    wp_deregister_script( 'myfunctions' );

     }
}

what am i missing? thanks!

  • Can you try the priority from 100 to default which is 10. just try add_action( 'wp_enqueue_scripts', 'my_deregister_javascript' ); – Maruti Mohanty Aug 19 '14 at 19:50
8

You have a basic mistake in your code. The following code

if ( !is_page('my-page') ) {

means that if you are not on that page, deregister the scripts. Have a look at the php operators

! ->  Not !$x True if $x is not true

Also, why not exclude this specific page when you initially enqueue these scripts, something like

add_action( 'wp_enqueue_scripts', 'my_register_javascript', 100 );

function my_register_javascript() {
   if ( !is_page('my-page') ) {

       // ENQUEUE/REGISTER SCRIPTS

     }
}

It is much cleaner and more efficient. I don't see the point in registerring a script and deregister it again. Rather register the scripts conditionally from the start

3

Finally got ONLY my front page to not load jquery, thought I would share it to possibly save some other hack 8 hrs of hit and miss.

// remove jquery from ONLY the front page

add_action( 'wp_enqueue_scripts', 'my_deregister_javascript' );
function my_deregister_javascript() {
   if ( is_home('front-page') ) {
        wp_deregister_script( 'jquery' );
   }
}

NOTE: is_page from the initial question does not work when you want to use front-page or home in the argument must use is_home (that cost me an hour)

2

I'd probably try only enqueueing those scripts on specific pages, rather than enqueuing and then dequeuing them:

function wpse158453_enqueue_scripts() {
    if(!is_page('my-page')) {
        wp_enqueue_script( 'jquery', '//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js' );
        wp_enqueue_script( 'jquery-ui', '//ajax.googleapis.com/ajax/libs/jqueryui/1.11.1/jquery-ui.min.js' );
        wp_enqueue_script( 'jquery-custom', get_template_directory_uri() . '/admin/js/custom.js' );
    }
}
add_action( 'wp_enqueue_scripts', 'wpse158453_enqueue_scripts' );

Otherwise you could try wp_dequeue_script rather than wp_deregister_script

Hope that helps.

  • 1
    I've added the ! operator to the code to align with the OP. I have upvoted the answer. If anyone objects, feel free to just rollback my change :-) – Pieter Goosen Aug 20 '14 at 5:08
0
//Remove jquery only on front page

add_action( 'wp_enqueue_scripts', 'my_deregister_javascript' );

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

add this script in your theme function.php file you will resolve query

  • only-code answers are not good. Please, add some explanation about what the code does so any user can understand it, – cybmeta Jun 21 '17 at 6:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.