1

With Wordpress you have good/bad plugins, some being much more economical than others. By economical I mean that they only call the CSS and JavaScript required for the function when the page is loaded where that function is enabled. Others don't discriminate and call the code on all pages. This can have a negative effect on page load speed/performance.

I have some plugins that are heavy in CSS and are laden with reems of jQuery/Javascript files - I only want them to be enabled on particular pages (not home). In hand I have the page ID and alias. Looking in the plugin folder I also see the main php file that includes all the JS / CSS. Within that file I tried something like is_page() but it seems to have no impact as if is_page has not yet been set.

    <?php if ( is_page( '3486' ) ) { exit; } ?>

exit on a line by itself kills the page indicating that the script is being called.

The question, "How and where do you place an if statement that will prevent the plugin CSS/JavaScript from being called on all pages but a particular one (or perhaps an array of pages)?

I could name the plugin but the question is really more generic to any plugin.

1 Answer 1

5

You can use wp_deregister_script, this will remove unwanted JS,CSS from specific pages.

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

function my_deregister_javascript() 
 { 
    if ( is_page('YOUR PAGE NAME') ) 
      {
        wp_deregister_script( 'WORDPRESS JS file NAME' ); 
      } 
 } 

Refer : https://codex.wordpress.org/Function_Reference/wp_deregister_script

Sign up to request clarification or add additional context in comments.

2 Comments

Using the function described it recognizes the page(s), good. In the particular plugin there are 20+ js calls. If however I add the wp_deregister to specific files such as wp_deregister_script( "allinone_bannerWithPlaylist.js" ); the line is ignored, meaning not deregistered. I tried with single ticks, dropping the .js suffix, etc. but it makes no difference. Looking at the function reference I copied the example verbatim, wp_deregister_script( 'jquery' ); and that works to disable all the .js lines. I don't understand whether I am getting the syntax wrong or other.
Okay I got it I think. I did not understand how the .js files were being called, the naming convention and assumed that the .js filename should work. I looked futher through the plugin file and at the specific names associated with enqueue, saw some like 'jquery-ui-accordion', 'jquery-ui-draggable', and 'lbg-all_in_one_bannerWithPlaylist'. Once I changed the deregister to those names it worked. wp_deregister_script( "lbg-all_in_one_bannerWithPlaylist" ); = good. Thx much.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.