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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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