1

With the reference of https://stackoverflow.com/questions/12463304/wordpress-load-plugin-css-js-in-functions-php-w-conditional-tags answered by @maiorano84

i am using cforms, FancyBox, WP Page Numbers plugins for Wordpress. From Page Speed point of view i want to remove CSS and JS of these plugin's from all pages except single post page. I also want to put the JS scripts in the footer. below is the code i have been trying but no luck. Would any body help me please?

if ( !is_single() ) {
add_action( 'wp_print_styles', 'my_deregisters', 100 );
add_action( 'wp_print_scripts', 'my_deregisters', 100 );

    function my_deregisters() {
        remove_action('wp_head', 'wp_page_numbers_stylesheet');
        wp_deregister_style( 'fancybox' ); 
        wp_deregister_script( 'fancybox' ); 
        wp_dequeue_script('fancybox');      
        wp_dequeue_style('fancybox'); 
        remove_action('wp_print_scripts', 'mfbfw_load');
        remove_action('wp_print_styles', 'mfbfw_css');
        remove_action('wp_head', 'mfbfw_init');
        remove_action('wp_head', 'cforms_style');

        }

} else {  }

1 Answer 1

1

Does it work any better added to a hook with the conditionals inside?

function my_deregisters() {

  if ( !is_single() ) {
   remove_action('wp_head', 'wp_page_numbers_stylesheet');
   //wp_deregister_style( 'fancybox' ); // i think this can be removed
   //wp_deregister_script( 'fancybox' ); // i think this can be removed
   wp_dequeue_script('fancybox');      
   wp_dequeue_style('fancybox'); 
   remove_action('wp_print_scripts', 'mfbfw_load');
   remove_action('wp_print_styles', 'mfbfw_css');
   remove_action('wp_head', 'mfbfw_init');
   remove_action('wp_head', 'cforms_style');
 }

} 
add_action( 'wp_enqueue_scripts', 'my_deregisters', 100 );
4
  • thanks @helgatheviking Kathy is Awesome link Nov 5, 2013 at 12:25
  • You are right. that was the mistake. it is working now but how can i load JS in the footer on single post page? Nov 5, 2013 at 12:27
  • One of the parameters of wp_enqueue_script allows you to load scripts in the footer, so any that are already set to $in_footer=true will load in the footer. If that isn't set, you'd have to deregister and reregister with that parameter changed. Seems like a hassle. Try one of the minification plugins (W3 total cache or WP Minify, etc) to combine the scripts and don't worry about them being in the header Nov 5, 2013 at 13:20
  • 1
    This is great, I have just compiled a set of PHP functions that makes it easier to handle more files using array and then looping over it - stramaxon.com/2021/10/… Oct 10, 2021 at 15:41

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.