0

Good evening, I'm not a php developer but I wrote this script (tested and fully working):

function deactivate_plugin_conditional() {
    if ( is_plugin_active('plugin-name/plugin.php') ) {
        require_once(ABSPATH .'/wp-admin/includes/plugin.php');
    deactivate_plugins('plugin-name/plugin.php');   
    }
}
add_action( 'admin_init', 'deactivate_plugin_conditional' );

sleep(1);

function activate_plugin_conditional() {
    if ( is_plugin_inactive('plugin-name/plugin.php') ) {
    require_once(ABSPATH .'/wp-admin/includes/plugin.php');
    activate_plugins('plugin-name/plugin.php');   
    }
}
add_action( 'admin_init', 'activate_plugin_conditional' );

I inserted it in functions.php theme, but doing so everytime some one (backed or front end) open the website it runs (obviously). I have to make it run in two specific page (imagine with id 321 and 322). To intercept a page from functions.php I wrote this code:

add_filter( 'template_include', function( $template ) {
    if ( is_page( array( 321, 322 ) ) ) 
        echo 'Here You are';
        else
    {
    echo 'KO';
    }

   return $template;
});

And this script works too, BUT if I try to merge theme....it doesn't work. Is there anyone that could help me to figure it out? Warm regards Ale

6
  • 1
    What's the purpose of the sleep function call? Note that deactivating and reactivating plugins at runtime is extremely unusual, and bad practice. You're clearly trying to solve a problem caused by a plugin on a page, maybe you should ask about that rather than trying to get this solution to work? By asking about this you've severely limited your options, and easier solutions to your original problem can't be posted as answers
    – Tom J Nowell
    Commented Nov 25, 2019 at 17:46
  • Good evenig Tom, first of thank You very much for Your answer. A sYou said the origina issue is caused by a plugin "Contact Form 7 - Stripe Extension" by ZealousWeb Technologies. If You activate it the website (frontend and backend) continue to run fast, BUT after 1/2 hours it cames back slow. We are inspected everything and the incredible thing is that on cPanel Cpu and ram sleep. Commented Nov 27, 2019 at 19:45
  • We are going out of head. The only solution that we have found is to deactivate manually the plugin and reactivate it but it's not a solution... The only one thing that we see under tools > site health is this warning : Errore: [] cURL error 28: Operation timed out after 10000 milliseconds with 0 bytes received. And on the server no error php are in the logs... Thank You for Your attention Commented Nov 27, 2019 at 19:45
  • I still don't know why you called sleep, I also don't see why this would solve your problem if it takes half an hour of cumulative time for it to slow down. I would ignore CPanel and focus on performance data from that page load that was slow, eitherway diagnosing issues with that plugin is the vendors support task not Stack Exchange, and I very much doubt that disabling and enabling will help. Your proposed solution also triggers a number of other potential problems, e.g. triggering activation/deactivation triggers that might cleanup tables
    – Tom J Nowell
    Commented Nov 27, 2019 at 22:57
  • Also the code in your question deactivates then immediatley reactivates the plugin. The call to sleep also implies a misunderstanding about how hooks work, as it won't delay the deactivation by any time at all
    – Tom J Nowell
    Commented Nov 27, 2019 at 22:59

0

Your Answer

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

Browse other questions tagged or ask your own question.