Ask your WordPress questions! Pay money and get answers fast! (more info)

Multisite - how to automatically deactivate another plugin? WordPress

I am the developer of Leaflet Maps Marker Plugin (mapsmarker.com) and I am currently working on a pro version which will be a separate plugin (/leaflet-maps-marker-pro/) and cant be active together with the free version (/leaflet-maps-marker/)

In order to achieve this, I added the following code on top of the pro version core file:

include_once( ABSPATH . 'wp-admin' . DIRECTORY_SEPARATOR . 'includes' . DIRECTORY_SEPARATOR . 'plugin.php' );
if (is_plugin_active('leaflet-maps-marker/leaflet-maps-marker.php') ) {
deactivate_plugins('leaflet-maps-marker/leaflet-maps-marker.php');
activate_plugin('leaflet-maps-marker-pro/leaflet-maps-marker.php', $redirect = 'plugins.php?activate=true');
}


This works fine on single site installation but fails on multisite. If fails on multisite if the free version is activated network wide and if the free version is active on a subblog only.

Therefore I tried the following:

if (is_plugin_active('leaflet-maps-marker/leaflet-maps-marker.php') ) {
global $wpdb;
set_time_limit(120);
$blogs = $wpdb->get_col("SELECT blog_id FROM {$wpdb->blogs} WHERE site_id = {$wpdb->siteid} AND spam = 0");
if ($blogs) {
foreach ($blogs as $blog_id) {
switch_to_blog($blog_id);
deactivate_plugins('leaflet-maps-marker/leaflet-maps-marker.php', true); //silently deactivate the plugin
restore_current_blog();
}
?><div id="message" class="updated fade"><p><span style="color:#FF3300;">Plugin</span><?php _e(' has been MASS DEACTIVATED.', 'pm'); ?></p></div><?php
} else {
?><div class="error"><p><?php _e('Failed to mass deactivate: error selecting blogs', 'pm'); ?></p></div><?php
}


Didnt work either. I would need the free version to be deactivated on all blogs (including the main one!) if the pro version gets activated as otherwise the user would get errors.

Can anyone tell me how to best achieve this?
thx

Answers (4)

2013-05-06

Yakir Sitbon answers:

I don't know about deactivate plugin and activate another one, but I think you can "disable" access to your plugin with MultiSite, And put "Error" notification for upgrade to pro plugin.

What you think about this idea?

[Nice to see you again. =)]


Robert Harm comments:

thanks, but that is unfortunately not an option due to other reasons...

2013-05-06

DHRUBA BHOWMIK answers:


Go to the Plugins => Add New page.
Enter 'Plugin Options Starter Kit' (without quotes) in the textbox and click the 'Search Plugins' button.
In the list of relevant Plugins click the 'Install' link for Plugin Options Starter Kit on the right hand side of the page.
Click the 'Install Now' button on the popup page.
Click 'Activate Plugin' to finish installation.
That's it!


Robert Harm comments:

thx but I need a code I can use in my plugin without users having to install another plugin...


DHRUBA BHOWMIK comments:

Before beginning
make a backup copy of your database.

see here:====================
Backing up your database as often as possible is essential. For WordPress, as well as for other applications, plugins and other scripts that help automate the task are easily obtainable. However, for several reasons, it is a good idea to understand the process of manually creating a backup copy of your database. This brief tutorial1 should help cement the process into a solid reference. We are assuming that you have an SQL database and have access via phpMyAdmin.

Before reading through the gory details written below, check out the screenshot to the left (click on the image). That picture is literally worth a thousand characters. Another way to avoid the forthcoming verbose explanation is to skip the next paragraph to the neatly summarized list.

First, open phpMyAdmin and select from the dropdown menu the database you wish to backup. If there is only one database available, select it by clicking on its name. Now, along the upper-right row of tabs, click on the "export" tab. From that screen, under the subcategory "Export", click on "select all" and make sure that the "SQL" option is selected. Then, under the SQL Options/Structure category, make sure "Structure", "Add DROP TABLE", "Add AUTO_INCREMENT value", and "Enclose table and field names with backquotes" are checked. Finally, under the "SQL Options/Data" category, make sure "Data", "Complete inserts", and "Use hexadecimal for binary fields" are checked. The "Export type" should be set to "INSERT". Finally, check "Save as file" and do not change the "File name template". It is advisable to save both a compressed copy and a zipped copy. Click "Save" and you are done.

Open phpMyAdmin, select database, click on "Export"
Within the "Export" screen, click on "Select All" and select "SQL"
Then check "Structure", "Add DROP TABLE", "Add AUTO_INCREMENT..", and "Enclose table.."
Also check "Data", "Complete inserts", and "Use hexadecimal.."
Check "Save as file", select a compression format, and "Go"!


Than
login to your database using phpMyAdmin (or whatever), and navigate to the “active_plugins” column of the “wp_options” table using the following SQL query (edit the default WordPress table prefix “wp_” if needed):


SELECT * FROM wp_options WHERE option_name = 'active_plugins';


Once the active_plugins column appears, click to edit it. You will see something similar to the following, depending on the number and type of plugins you have installed:

a:31:{i:0;s:13:"AddMySite.php";i:1;s:19:"akismet/akismet.php";i:2;s:23:"all_in_one_seo_pack.php";i:3;s:16:"authenticate.php";i:4;s:28:"breadcrumb-navigation-xt.php";i:5;s:18:"codeautoescape.php";i:6;s:37:"contact-coldform/contact_coldform.php";i:7;s:32:"custom-query-string-reloaded.php";i:8;s:30:"customizable-post-listings.php";i:9;s:33:"dd-sitemap-gen/dd-sitemap-gen.php";i:10;s:20:"download-counter.php";i:11;s:13:"feedcount.php";i:12;s:13:"full_feed.php";i:13;s:15:"get-weather.php";i:14;s:36:"google-sitemap-generator/sitemap.php";i:15;s:13:"gravatars.php";i:16;s:19:"kill-admin-nags.php";i:17;s:18:"landingsites13.php";i:18;s:30:"nofollow-free/nofollowfree.php";i:19;s:17:"ol_feedburner.php";i:20;s:16:"plugins-used.php";i:21;s:22:"popularity-contest.php";i:22;s:39:"search-everything/search_everything.php";i:23;s:27:"simple-tags/simple-tags.php";i:24;s:26:"simple_recent_comments.php";i:25;s:18:"simple_twitter.php";i:26;s:25:"subscribe-to-comments.php";i:27;s:24:"the-excerpt-reloaded.php";i:28;s:18:"theme-switcher.php";i:29;s:9:"top10.php";i:30;s:16:"wp-db-backup.php";}


That entire array of code represents every active plugin on your site. Thus, to quickly disable all plugins without using the WP Admin area, highlight the entire block of code, cut it out, and paste it into a safe, offline text file. After removing the code, click the button to save your changes and that’s it. All WordPress plugins are now deactivated (yet still installed, and with all plugin options intact). This obviously is a huge time-saver that really comes in handy during those mission-critical, time-sensitive situations where every second counts. Once you are ready to re-activate your entire set of plugins, simply cut/copy & paste the preserved code back into the “active_plugins” field. Click save and done. Again, don’t forget to backup your database before editing it ;)



Alternately, here is a one-second query to disable all plugins:----------------------------------------

UPDATE wp_options SET option_value = '' WHERE option_name = 'active_plugins';

Upon execution, this query will clear the active_plugins field of all active plugins (duh), effectively disabling (without uninstalling or modifying) the entire set. This method is great if you plan on re-enabling each plugin individually, say, after resolving some heinous server error. Whereas the previous technique makes it easy to re-enable all plugins en masse, this query is perfect for simply “nuking” all active plugins with no remorse. ;)


Updated Method========================

Apparently, this method works only for WordPress versions less than 2.9. For 2.9 and better, use this instead:

UPDATE wp_options SET option_value = 'a:0:{}' WHERE option_name = 'active_plugins';

So use that if working with WP2.9 or beyond!

I hope your problem solve If you follow my post.

Thanks


Robert Harm comments:

thx again. Unfortunately this is also not what I need - only my free plugin should be deactivated when the user activates the pro version. This means the solution must work automatically especially for users who only activate plugins through the network admin setting without touching any code...

2013-05-07

Daniel Yoen answers:

Hello, you can try this :

add_filter('site_option_active_sitewide_plugins', 'node_deactivate_plugins');

function node_deactivate_plugins($value)
{
unset($value['leaflet-maps-marker/leaflet-maps-marker.php']);

return $value;
}


hope this help :-)


Daniel Yoen comments:

also you can read this : http://wordpress.stackexchange.com/a/25979

2013-05-08

Giri answers:

Just use filters to disable old plugin.

<?php

//single
add_filter('option_active_plugins', 'maps_marker_activation');

//multisite
add_filter('site_option_active_sitewide_plugins', 'maps_marker_activation');

function maps_marker_activation($value) {

if (is_plugin_active('leaflet-maps-marker/leaflet-maps-marker.php') ) {
unset($value['leaflet-maps-marker/leaflet-maps-marker.php']);
return $value;
}
?>


Then proceed with plugin activation as usual.