---
title: "Automated reactivation of plugins"
description: "The problem. Although we have not yet been able to find the exact cause, we have two customers to whom a more than strange thing happens, which is that they have a plugin, different in both installations, that deactivates when"
url: https://artprojectgroup.com/automated-reactivation-of-plugins
date: 2023-05-23
modified: 2023-05-23
author: "Art Project Group"
image: https://artprojectgroup.com/wp-content/uploads/2023/02/wordpress-blog.webp
categories: ["Tutorials"]
tags: ["functions.php", "PHP", "WordPress"]
type: post
lang: en-US
---

# Automated reactivation of plugins

### The problem

Although we have not yet been able to find the exact cause, we have two customers to whom a more than strange thing happens, which is that they have a plugin, different in both installations, that deactivates when it updates. That is a minor problem in an unimportant plugin, but in a plugin that synchronises WooCommerce orders with an ERP, or a payment method, it is more than a problem that it deactivates and does not reactivate by itself, as should happen naturally, by the way.

In spite of the fact that we have reviewed everything that can be reviewed and that one of the plugins is of the creation of **APG**, **WC – Cetelem payment gateway**, so we have its code more than under control and we know that it does not cause any kind of problem in hundreds of other installations, we have not managed to find the cause that makes these two plugins in particular, and no other of those that make up both websites, not reactivate automatically.

### The solution

To solve it we have had to create custom code that takes care of automatically reactivating the plugin that we have monitored.

If you come across the same problem you only have to add this small PHP script in the `functions.php` file of your child theme:

Copy to clipboardSyntax Highlighter/* Auto-activate plugin */
function apg_activate_plugin() {
$path = 'plugin-folder/plugin-file.php';
if ( ! is_plugin_active( $path ) ) {
$result = activate_plugin( $path );
$plugin = 'Plugin name';
if ( ! is_wp_error( $result ) ) {
add_action( 'admin_notices', function() use ( $plugin ) {
echo '' . sprintf( 'The **%s** plugin is required and has been activated automatically.', $plugin ) . '

';
} );
} else {
add_action( 'admin_notices', function() use ( $plugin ) {
echo '' . sprintf( 'The **%s** plugin could not be activated automatically.', $plugin ) . '

';
} );
}
}
}
add_action( 'admin_init', 'apg_activate_plugin' );
Where you will have to customise the path of the plugin, indicating the one you need to monitor, for example the path of WooCommerce is `woocommerce/woocommerce.php`, and the name of the plugin that will be shown in the on-screen messages.

### But what if the plugin folder changes its name every time it updates?

Here you have a problem, since you cannot monitor it with the PHP code proposed above, and this particular situation is the one that occurs with our plugin **WC – Cetelem payment gateway,** where the installed version is added after the name of the folder

To solve it we have had to create this other PHP code:

Copy to clipboardSyntax Highlighter/* Auto-activate plugin */
function apg_activate_plugin() {
$plugins = scandir( WP_PLUGIN_DIR );
foreach ( $plugins as $plugin ) {
if ( strpos( $plugin, 'woocommerce-cetelem-payment-gateway' ) !== false ) {
$path = $plugin . '/cetelem-payment.php';
}
}
if ( !is_plugin_active( $path ) ) {
$result = activate_plugin( $path );
$plugin = 'WC - Cetelem payment gateway';
if ( !is_wp_error( $result ) ) {
add_action( 'admin_notices', function ()use( $plugin ) {
echo '' . sprintf( 'The **%s** plugin is required and has been activated automatically.', $plugin ) . '

';
} );
} else {
add_action( 'admin_notices', function ()use( $plugin ) {
echo '' . sprintf( 'The **%s** plugin could not be activated automatically.', $plugin ) . '

';
} );
}
}
}
add_action( 'admin_init', 'apg_activate_plugin' );
Where the name of the folder in which the plugin is installed is searched for in the plugins directory, starting from the fixed part of its name, so as to be able to reactivate it again whatever the folder is called.

Now then, we do not recommend using this code just as we show it, since it is going to slow down the execution of the WordPress administration panel and the *ADMIN-AJAX*, but rather we recommend running it every five minutes by adding it to a scheduled event, assuming that you have the *WP-CRON* under control and you run it every five minutes; if not, you are already late in doing so.

Therefore the best way to use it is like this:

Copy to clipboardSyntax Highlighter/* Auto-activate Cetelem */
function apg_cron_minutes( $schedules ) {
if ( ! isset( $schedules[ "5_minutes" ] ) ) {
$schedules[ "5_minutes" ] = array(
'interval' => 5 * 60,
'display' => __( 'Every 5 minutes' ) );
}

return $schedules;
}
add_filter( 'cron_schedules', 'apg_cron_minutes' );

function apg_schedule_activation() {
if ( ! wp_next_scheduled( 'apg_activate_cetelem' ) ) {
wp_schedule_event( time(), '5_minutes', 'apg_activate_cetelem' );
}
}
add_action( 'wp', 'apg_schedule_activation' );

function apg_activate_cetelem_plugin() {
$plugins = scandir( WP_PLUGIN_DIR );
foreach ( $plugins as $plugin ) {
if ( strpos( $plugin, 'woocommerce-cetelem-payment-gateway' ) !== false ) {
$path = $plugin . '/cetelem-payment.php';
}
}
if ( ! is_plugin_active( $path ) ) {
$result = activate_plugin( $path );
$plugin = 'WC - Cetelem payment gateway';
if ( ! is_wp_error( $result ) ) {
add_action( 'admin_notices', function ()use( $plugin ) {
echo '' . sprintf( 'The **%s** plugin is required and has been activated automatically.', $plugin ) . '

';
} );
} else {
add_action( 'admin_notices', function ()use( $plugin ) {
echo '' . sprintf( 'The **%s** plugin could not be activated automatically.', $plugin ) . '

';
} );
}
}
}
add_action( 'apg_activate_cetelem', 'apg_activate_cetelem_plugin' );
This way you make sure that the plugin will not stay deactivated beyond five minutes and that the performance of your WordPress does not suffer.

You can also use code very similar to the previous one to make sure that your most fundamental plugins do not stay deactivated beyond 5 minutes by adding an array with the list of plugins you want to check, but we leave that to your ingenuity and creativity; as you can see, the possibilities for control over plugins that open up are multiple.

We hope you find this code useful and, as always, we look forward to your comments.
