Created
March 31, 2026 06:01
-
-
Save RichardNesbitt/e7cd64ea25db46bf319c589ed510fa4b to your computer and use it in GitHub Desktop.
Useful WordPress tools
This file contains hidden or 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 | |
| // place a database dump and this file in the root directory of the WordPress site | |
| // to be able to import the .sql file into the active datbase. | |
| // You trigger this script by visiting https://yourdomain.com/db-import.php?key=mysecret123 | |
| ## SET THESE VARIABLES ACCORDINGLY | |
| $allowed_key = 'mysecret123'; | |
| $sqlFile = __DIR__ . '/your_database_dump.sql'; | |
| require_once __DIR__ . '/wp-config.php'; | |
| // Optional: remove this if you don't want the key check | |
| if (!isset($_GET['key']) || $_GET['key'] !== $allowed_key) { | |
| http_response_code(403); | |
| exit('Forbidden'); | |
| } | |
| // Increase limits | |
| set_time_limit(0); | |
| ini_set('memory_limit', '512M'); | |
| // Connect | |
| $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME); | |
| if ($mysqli->connect_error) { | |
| die('Connection failed: ' . $mysqli->connect_error); | |
| } | |
| // Disable FK checks (important for dropping tables) | |
| $mysqli->query('SET FOREIGN_KEY_CHECKS=0'); | |
| // Drop all tables | |
| $result = $mysqli->query("SHOW TABLES"); | |
| while ($row = $result->fetch_row()) { | |
| $table = $row[0]; | |
| $mysqli->query("DROP TABLE IF EXISTS `$table`"); | |
| } | |
| $mysqli->query('SET FOREIGN_KEY_CHECKS=1'); | |
| echo "All tables dropped...\n"; | |
| // Import SQL file | |
| $sqlFile = __DIR__ . '/your_database_dump.sql'; | |
| if (!file_exists($sqlFile)) { | |
| die("SQL file not found: $sqlFile"); | |
| } | |
| $handle = fopen($sqlFile, 'r'); | |
| if (!$handle) { | |
| die("Could not open SQL file"); | |
| } | |
| $query = ''; | |
| while (($line = fgets($handle)) !== false) { | |
| $line = trim($line); | |
| // Skip comments | |
| if ($line === '' || strpos($line, '--') === 0 || strpos($line, '/*') === 0) { | |
| continue; | |
| } | |
| $query .= $line . ' '; | |
| // End of statement | |
| if (substr($line, -1) === ';') { | |
| if (!$mysqli->query($query)) { | |
| echo "Error executing query:\n$query\n"; | |
| echo "MySQL error: " . $mysqli->error . "\n"; | |
| fclose($handle); | |
| exit; | |
| } | |
| $query = ''; | |
| } | |
| } | |
| fclose($handle); | |
| $mysqli->close(); | |
| echo "Import complete."; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment