I recently had the need to programmatically activate and deactivate WordPress plugins from within a custom admin UI (theme). It took me quite a lot of digging to figure out exactly how, so I figured I’d share it here.
Note that this requires wp-admin/includes/plugin.php to be included if you’re not running from within WordPress.
function toggle_plugin() {
// Full path to WordPress from the root
$wordpress_path = '/full/path/to/wordpress/';
// Absolute path to plugins dir
$plugin_path = $wordpress_path.'wp-content/plugins/';
// Absolute path to your specific plugin
$my_plugin = $plugin_path.'my_plugin/my_plugin.php';
// Check to see if plugin is already active
if(is_plugin_active($my_plugin)) {
// Deactivate plugin
// Note that deactivate_plugins() will also take an
// array of plugin paths as a parameter instead of
// just a single string.
deactivate_plugins($my_plugin);
}
else {
// Activate plugin
activate_plugin($my_plugin);
}
}
Sorry for my lack of knowledge but where do i need to put this code on ? which .php?
Thanks in advance
It really depends what you’re trying to do. You need to trigger it somehow, so if, for example, you’re creating a webpage that users can activate/deactivate a particular plugin from, you’d have a link on that page that loads a php file with this code in it. If you could explain your use case I might be able to give you a better answer.
you see this is my problem i design http://www.dream2befit.com if u want to enter, then you will see
Fatal error: Class ‘Cart66Common’ not found in /home/hybridi1/public_html/dream2befit.com/wp-content/themes/dream2befit/functions/functions.php on line 1979
i accidently deactive the plugin in wordpress and now i can’t see the website or enter wordpress so i was looking
for a solution to activate the plugin again but programmatically since i can’t login to wordpress :/
I would probably start by editing your functions.php file and removing (or commenting out) that reference. However if you wanted to programmatically re-activate the plugin just create a php file (let’s call it cart66activate.php) and drop it in your site somewhere. Then add the above code to it (making sure that you have all the paths correct). So in cart66activate.php you’d have the toggle_plugin() function, then underneath that just call it.
include('/path/to/wordpress/' . 'wp-admin/includes/plugin.php'); function toggle_plugin() { ... } toggle_plugin();Then just hit the page from a browser. Ex. http://www.dream2befit.com/cart66activate.php
If you need additional help send me an email through the contact form and maybe we can sort it out.
Garth i will try it wish me luck so it will activate the plugin, and by make sure my paths are correct, should i put
$wordpress_path = ‘http://www.dream2befit.com/’; as my wordpress path, or should i need to put
$wordpress_path = ‘/home/hybridi1/public_html/dream2befit.com/’; <- since i have wordpress installed in root?
thanks in advance
for your help n_n i should invite you beer
if everything works
The second one – /home/hybridi1/public_html/dream2befit.com/. These functions require file system paths, not URIs.
This is the code i put and enter the page to activate it but it didn’t work
still not activated :/
require('/home/hybridi1/public_html/dream2befit.com/wp-admin/includes/plugin.php'); function toggle_plugin() { // Full path to WordPress from the root $wordpress_path = '/home/hybridi1/publichtml/dream2befit.com/'; // Absolute path to plugins dir $plugin_path = $wordpress_path.'wp-content/plugins/'; // Absolute path to your specific plugin $my_plugin = $plugin_path.'cart66-lite/cart66.php'; // Check to see if plugin is already active if(is_plugin_active($my_plugin)) { // Deactivate plugin // Note that deactivate_plugins() will also take an // array of plugin paths as a parameter instead of // just a single string. deactivate_plugins($my_plugin); } else { // Activate plugin activate_plugin($my_plugin); } }Try changing
to
with an underscore.
Hi Garth
Im the new webmaster for this site, and got this error again do you know if this error got fixed.
I got the following error when trying to run the script
“Fatal error: Call to undefined function is_plugin_active()”
Thanks !!