0

I use some "heavy duty" plugging, like syntaxhighlighter, GD star rating etc. i create a de-register.php file which i use to deregister scripts and styles whenever they are not used.

Example:

add_action( 'wp_print_styles', 'my_deregister_styles', 100 );
function my_deregister_styles() {

    if (is_home()) {
        wp_deregister_style('bbp-default');
        wp_dequeue_style('bbp-default');
        wp_deregister_style('bbp-default-rtl');
        wp_dequeue_style('bbp-default-rtl');

        wp_deregister_style('edd-styles');
        wp_dequeue_style('edd-styles');

        wp_deregister_style('onp-sociallocker');
        wp_dequeue_style('onp-sociallocker');
    }
}

In this page i have about 200 lines since i am de-registering the css and js files of 5 plugins (or so) when ever they are not being used.

For that end i created some checkbox metaboxes in each post where i can click and say "i use rating here" (for example).

My question is.
Is my way wrong? Does the whole process of using wp_deregister_style and wp_deregister_script defeats the purpose?

Does the actual use of those actions slows my site down? Is there a better way to only use plugins and the styles and scripts they come with when i need them and only then?

I have of course read and searched but couldn't find anything else.
Would love to hear of any solution you think might fit.

1

I assume that you want to activate a plugin only when it is necessary. There is a better option to do this. You can activate the whole plugin upon a certain action. I have provided a example code that will help you achieve what you are looking for and if you have any more queries you can put a comment. If it works for you can accept the answer.

function run_activate_plugin( $plugin ) {
     $current = get_option( 'active_plugins' );
     $plugin = plugin_basename( trim( $plugin ) );

         if ( !in_array( $plugin, $current ) ) {
           $current[] = $plugin;
           sort( $current );
           do_action( 'activate_plugin', trim( $plugin ) );
           update_option( 'active_plugins', $current );
           do_action( 'activate_' . trim( $plugin ) );
           do_action( 'activated_plugin', trim( $plugin) );
           } 

         return null;
       }
         run_activate_plugin( 'akismet/akismet.php' );
  • i have to do some testing ;) I am trying to lower the load time - and trying to asertaine the affect of de-registering stuff vs other solutions. – Sagive SEO Sep 12 '14 at 16:42
  • thanks for your input, its not what i what searching for but an intresting answer (and the only suggestion). Cheers, Sagive. – Sagive SEO Jan 3 '15 at 14:12

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.