0

please how do I activate a plugin after a set period of time automatically. For example, let's say I want to activate Jetpack automatically after 3 weeks. Thanks

6
  • In order to do this, you need to put in some code in another (active) plugin or in the functions.php file of your active (child) theme. Would that work for you?
    – mukto90
    Commented Aug 13, 2021 at 16:03
  • Yes but what is the code Commented Aug 13, 2021 at 16:15
  • I would note that this can't be done via code inside the plugin that you want to activate as that would require activation. What problem are you trying to solve by doing this? What's the context behind this? What are the constraints? Also note that you're more likely to be told how to do it, rather than getting a copy paste code snippet for it. The solution for this may not involve PHP code, or it might require a specific plugin, but knowing which solution is appropriate to your use case is difficult without more context/info
    – Tom J Nowell
    Commented Aug 13, 2021 at 16:16
  • I just need a code to add to my child theme to simply activate the plugin, in this case Jetpack, after a specified period of time. I just want the plugin activated, everything else will happen automatically at plugin activation Commented Aug 13, 2021 at 16:19
  • why? Normally people would put an admin notice with a link to install Jetpack, auto-activation is frowned upon ( especially since Jetpack would require you to manually connect it to wp.com for most of it's features anyway so it wouldn't work how you want it ). But why after a 3 week delay? Is Jetpack the plugin you're activating or just an example? Do you have control over this other plugin? I know it might not seem relevant to you, but humor me, the worst that happens is I say "oooh that makes sense" and nobody asks again
    – Tom J Nowell
    Commented Aug 13, 2021 at 17:21

1 Answer 1

0

Add this to the functions.php file of your active (child) theme-

add_action( 'init', 'wpse_393267_activate_plugin' );
function wpse_393267_activate_plugin() {

    if( !function_exists( 'is_plugin_active' ) ) {
        include_once ABSPATH . '/wp-admin/includes/plugin.php';
    }

    $plugin_to_activate = 'jetpack/jetpack.php'; // plugin-dir/plugin-file.php
    $date_to_activate   = '2021-08-13'; // YYYY-MM-DD

    if( is_plugin_active( $plugin_to_activate ) ) return;

    if( time() >= strtotime( $date_to_activate ) ) {
        activate_plugin( $plugin_to_activate );
    }
}

Change the $plugin_to_activate and $date_to_activate as you want.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.