Skip to content

Instantly share code, notes, and snippets.

@ka215
Created June 16, 2017 14:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ka215/1f1ba4d05aca56c4edefd3e6649a16ed to your computer and use it in GitHub Desktop.
Save ka215/1f1ba4d05aca56c4edefd3e6649a16ed to your computer and use it in GitHub Desktop.
This is the tool for WordPress to manually deactivate any plugins and rollback to default theme.
<?php
/**
* WordPress Emergency Tools v1.0
*
* This is a tool to manually deactivate a theme and any plugins when you can not
* access the admin panel due to WordPress theme or plugins bug etc.
* Usage it please place this file in the same directory as "wp-config.php" and
* access it directly from the browser.
* Please delete this file promptly after use (If this file will remain, it will
* be a serious security hole for your service).
*
* Note: The author is not responsible for any trouble that occurred using this tool.
*
* @author: ka2
*
*/
if ( ! defined( 'WP_DEFAULT_THEME' ) )
define( 'WP_DEFAULT_THEME', 'twentyseventeen' );
session_start();
require_once( dirname(__FILE__) . '/wp-config.php' );
function dbConnect() {
try {
$_host = explode( ':', DB_HOST );
$_dsn = 'mysql:';
$_dsn .= 'host=' . $_host[0] . ';';
$_dsn .= ! empty( $_host[1] ) ? 'port=' . $_host[1] . ';' : '';
$_dsn .= 'dbname=' . DB_NAME .';';
$_dsn .= 'charset=' . DB_CHARSET;
$db = new PDO( $_dsn, DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_EMULATE_PREPARES => false
] );
return $db;
} catch ( PDOException $e ) {
die( 'Database connection failed.' . $e->getMessage() );
}
}
$db = dbConnect();
$result = '';
if ( ! empty( $_POST ) ) {
$result .= deactivateComponents( $db, $table_prefix );
}
$result .= listComponents( $db, $table_prefix );
function listComponents( $db, $table_prefix ) {
$components = array();
// Get active plugins
$sql = "SELECT option_value FROM {$table_prefix}options WHERE option_name = 'active_plugins'";
$query = $db->query( $sql, PDO::FETCH_ASSOC );
$res = $query->fetch();
$components['plugins'] = unserialize( $res['option_value'] );
// Get current theme
$sql = "SELECT option_value FROM {$table_prefix}options WHERE option_name = 'current_theme'";
$query = $db->query( $sql, PDO::FETCH_ASSOC );
$res = $query->fetch();
$components['theme'] = $res['option_value'];
// Create list
$doc = '<form method="post" action="'. $_SERVER['PHP_SELF'] .'">';
$doc .= '<ul style="list-style-type: none;">';
$doc .= '<li><strong>Active Plugins</strong></li>';
foreach ( $components['plugins'] as $_plugin ) {
$doc .= '<li><label><input type="checkbox" name="plugins[]" value="'. $_plugin .'">'. substr( $_plugin, 0, strpos( $_plugin, '/' ) ) .'</label></li>';
}
$doc .= '<li><hr></li>';
$doc .= '<li><strong>Current Theme</strong></li>';
if ( ! empty( $components['theme'] ) ) {
$doc .= '<li><label><input type="checkbox" name="theme" value="'. $components['theme'] .'">'. $components['theme'] .'</label></li>';
} else {
$doc .= '<li><span style="color:grey;margin-left:1em;">Undefiend</span></li>';
}
$doc .= '</ul>';
$doc .= '<p>Submit after checked the components you want to deactivate.</p>';
$doc .= '<input type="submit" value="Submit">';
$doc .= '</form>';
return $doc;
}
function deactivateComponents( $db, $table_prefix ) {
$message = array();
if ( array_key_exists( 'plugins', $_POST ) ) {
$sql = "SELECT option_value FROM {$table_prefix}options WHERE option_name = 'active_plugins'";
$query = $db->query( $sql, PDO::FETCH_ASSOC );
$res = $query->fetch();
$active_plugins = unserialize( $res['option_value'] );
foreach ( $active_plugins as $_i => $_plugin ) {
if ( in_array( $_plugin, $_POST['plugins'] ) ) {
unset( $active_plugins[$_i] );
}
}
$active_plugins = serialize( $active_plugins );
$stmt = $db->prepare( "UPDATE {$table_prefix}options SET option_value = :value WHERE option_name = 'active_plugins'" );
$stmt->bindParam( ':value', $active_plugins );
if ( $stmt->execute() ) {
$message[] = '<p style="color:green;">Deactivated the specified plugins.</p>';
} else {
$message[] = '<p style="color:red;">Could not deactivate the plugins.</p>';
}
}
if ( array_key_exists( 'theme', $_POST ) ) {
$default_theme = WP_DEFAULT_THEME;
$stmt1 = $db->prepare( "UPDATE {$table_prefix}options SET option_value = :theme WHERE option_name = 'current_theme'" );
$stmt1->bindParam( ':theme', $default_theme );
$stmt2 = $db->prepare( "UPDATE {$table_prefix}options SET option_value = :theme WHERE option_name = 'template'" );
$stmt2->bindParam( ':theme', $default_theme );
$stmt3 = $db->prepare( "UPDATE {$table_prefix}options SET option_value = :theme WHERE option_name = 'stylesheet'" );
$stmt3->bindParam( ':theme', $default_theme );
if ( $stmt1->execute() && $stmt2->execute() && $stmt3->execute() ) {
$message[] = '<p style="color:green;">Deactivated the current theme.</p>';
} else {
$message[] = '<p style="color:red;">Could not deactivate the current theme.</p>';
}
}
session_destroy();
return implode( "\n", $message );
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>WP Emergency Tool</title>
</head>
<body>
<p>Now connecting to database of WordPress.</p>
<?php echo $result; ?>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment