0

I'm trying to disable the load of nextgen gallery 2.x styles and scripts, I used

function mytheme_deregister_scripts_and_styles(){
  $unwanted_scripts = array(
    'photocrati_ajax'
  );
  foreach ( $unwanted_scripts as $script ) {
    wp_dequeue_script( $script );
    wp_deregister_script( $script );
  }

  $unwanted_styles = array(
    'photocrati-nextgen_basic_compact_album'
  );
  foreach ( $unwanted_styles as $style ) {
    wp_dequeue_style( $style );
    wp_deregister_style( $style );
  }
    // to display their handles
/*  global $wp_scripts;
    echo '<div style="position:absolute; left:200px; top:200px; background:white; z-index:100;">';
    foreach( $wp_scripts->queue as $handle ) :
        echo "<p>";
        echo $handle;
        echo "</p>";
    endforeach; 
    echo "</div>"; */ 
}
add_action( 'wp_enqueue_scripts', 'mytheme_deregister_scripts_and_styles', 99 );
**SCRIPTS AND STYLES LIST:**

However, nothing changed the styles are still being loaded. What is wrong with the code, and how to fix it?

  • Have you verified those scripts and styles are hooked onto wp_print_styles? I'd bet they're actually hooked to wp_enqueue_scripts. – Andrew Bartel Jan 26 '15 at 23:12
  • They are actually plugins stuff, I had to print them out so I can get their handle name to skip them, but the code doesn't work – Murhaf Sousli Jan 26 '15 at 23:13
0

Try the following:

function mytheme_deregister_scripts_and_styles(){
  $unwanted_scripts = array(
    'photocrati_ajax',
    'photocrati-nextgen_basic_thumbnails',
    'photocrati-nextgen_basic_extended_album'
  );
  foreach ( $unwanted as $script ) {
    wp_dequeue_script( $script );
    wp_deregister_script( $script );
  }

  $unwanted_styles = array(
    'style_1',
    'style_2'
  );
  foreach ( $unwanted_styles as $style ) {
    wp_dequeue_style( $style );
    wp_deregister_style( $style );
  }
}
add_action( 'wp_enqueue_scripts', 'mytheme_deregister_scripts_and_styles', 99 );

It looks like you may not have it quite right:

wp_deregister_script('photocrati_ajax');
wp_deregister_style('photocrati-nextgen_basic_compact_album');
wp_dequeue_style( 'photocrati-nextgen_basic_compact_album' );

If they have already been registered and enqueued, I'd make sure to both deregister and dequeue them.

Also, make sure to use the _script commands on the scripts and the _style commands on the styles.

If they are not using wp_enqueue_styles...

You can remove items added via other actions and filters by using the remove_filter and remove_action items after they are called but before they are fired. (if the add_actions are placed on init, then you could put your actions to remove them later on the init hook)

If they manually draw them into the plugin Then that is just bad design ... and you will likely have to edit their plugin and just comment out the place they add them at.

  • I tried your code after fixing $wanted variable in the first foreach, however it didn't skip any style or script! – Murhaf Sousli Jan 27 '15 at 2:06
  • Have you checked to see what hook they are using to enqueue scripts? Standard practice is to use the wp_enqueue_scripts hook ... but if they are doing their on a later hook that could well be the problem. Some plugins even simply write out their script and css links using the wp_head action. If the above doesn't work maybe they are doing something odd. – Privateer Jan 27 '15 at 2:54
  • How can I know how are they hooked in the plugins! nextgen styles are loaded in the header section, while its scripts are loaded in the footer section – Murhaf Sousli Jan 27 '15 at 17:10
  • Use a good text editor and do a directory wide search for those values in the plugin directory in question. That should show how they are being added. e.g. search for photocrati_ajax or photocrati-nextgen_basic – Privateer Jan 27 '15 at 18:25
  • The code is fine, the problem is not all styles are hooked using wp_enqueue, is there other way to skip these styles? – Murhaf Sousli Jan 27 '15 at 20:05

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.