Skip to content

Instantly share code, notes, and snippets.

@stanwmusic
Forked from solepixel/activate-plugin.php
Created February 19, 2018 09:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stanwmusic/f58861924812cc9b74b781855ba8664d to your computer and use it in GitHub Desktop.
Save stanwmusic/f58861924812cc9b74b781855ba8664d to your computer and use it in GitHub Desktop.
Originally design to activate Andrew Norcross's Airplane Mode plugin (https://github.com/norcross/airplane-mode), this little script will allow you to activate a plugin when the WordPress admin plugin screen cannot be loaded, for whatever reason.
<?php
/**
* This file will activate the Airplane Mode plugin if not already activated. This is helpful when you do not have internet connection and you're unable to reach the WP Admin Plugins page to activate the plugin.
*/
// get WP bootstrap
define('WP_USE_THEMES', false);
require(__DIR__ . '/wp/wp-blog-header.php');
// Set the name and plugin slug of the plugin you wish to activate
$plugin_name = 'Airplane Mode';
$plugin_slug = 'airplane-mode/airplane-mode.php';
/**
*
* CHANGE ANYTHING BEYOND THIS POINT AT YOUR OWN RISK
*
*/
// make the output pretty
echo '<style>
* {
-webkit-font-smoothing: subpixel-antialiased;
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
color:#444;
font-size:14px;
}
body {
padding:25px;
text-align:center;
margin:30px auto;
width:800px;
max-width:90%;
overflow:auto;
}
</style>';
// check to be sure the plugin is in the plugins folder
if( ! file_exists( WP_PLUGIN_DIR . '/' . $plugin_slug ) ){
printf( '%s has not been installed. Make sure you have uploaded/copied the plugin into the plugins folder (%s). If you have ensured this has been done correctly, double check that the plugin slug is correct: %s',
$plugin_name,
WP_PLUGIN_DIR,
$plugin_slug
);
exit();
}
global $wpdb;
// get active_plugins option value (serialized array) and unserialize it
$select = "SELECT `option_value` FROM {$wpdb->options} WHERE `option_name` = 'active_plugins';";
$active_plugins = $wpdb->get_var( $select );
$active_plugins_array = unserialize( $active_plugins );
// make sure it's not already activated
if( ! in_array( $plugin_slug, $active_plugins_array ) ){
// add airplane mode to the active plugins array
$active_plugins_array[] = $plugin_slug;
$active_plugins = serialize( $active_plugins_array );
// update options table
$wpdb->update( $wpdb->options, array(
'option_value' => $active_plugins
), array(
'option_name' => 'active_plugins'
));
// alert user of outcome
echo $plugin_name . ' Activated!';
exit();
}
// alert user of outcome
echo $plugin_name . ' is already activated.';
exit();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment