0
votes

I develop plugin for WordPress, and it represented in two versions: Free and Pro. For licensing used Freemius.

I've got many messages in support like a: "I can't activate Pro plugin, due to error: Cannot declare class A, because the name is already in use".

So, users don't understand, that Free ver. need to be deactivated, before they activate Pro. Because it's look like user want to activate two plugins at same time.

I want to programmatically deactivate Free plugin, in process of activating Pro.

Of course, I tried to use deactivate_plugins() functions, and hook it in "register_activation_hook", but:

If I try to deactivate (for example Akismet) - it's working. I'm activate my plugin - Akismet is deactivating, but when I have activated my Free ver. of plugin, and tried to activate Pro - I getting same error - Cannot declare class ...

It's look like, that register_activation_hook fire too late, and it have no time to deactivate Free before.

I'm sure this problem is being solved, but I can't figure out how. Please, share your experience.

1

1 Answers

0
votes

I'm not found solution for deactivating plugin as I need, but I found another way:

In index file of my plugin:

$plugin_path = 'my_plugin/index.php';

if ( function_exists('is_plugin_active') && is_plugin_active($plugin_path)) {
    wp_die("Before activating Pro version, please, deactivate Free version. You can't activate it both.");
}

This "trick" allow me to avoid non-informative error for users (about redeclaration), and provide most useful message with instructions.