Skip to content

Instantly share code, notes, and snippets.

@devuri
Created February 22, 2023 13:28
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 devuri/ccd0a5085afb10f5522516b51e9ffdd5 to your computer and use it in GitHub Desktop.
Save devuri/ccd0a5085afb10f5522516b51e9ffdd5 to your computer and use it in GitHub Desktop.
Find and Replace a Domain in a WordPress Database
<?php
/**
* Plugin Name: Domain Replacer
* Plugin URI: https://www.example.com
* Description: A simple plugin to replace one domain with another domain in the WordPress database
* Version: 1.0
* Author: Your Name
* Author URI: https://www.example.com
*/
defined( 'ABSPATH' ) || exit;
/**
* Define the path to the plugin directory
*/
define( 'DOMAIN_REPLACER_DIR', plugin_dir_path( __FILE__ ) );
/**
* Include the find_replace_domain_in_database() function
*/
function find_replace_domain_in_database( $old_domain, $new_domain ) {
global $wpdb;
$columns_query = $wpdb->prepare(
"SELECT CONCAT(
'UPDATE `', TABLE_SCHEMA, '`.`', TABLE_NAME, '` SET `', COLUMN_NAME, '` = REPLACE(`', COLUMN_NAME, '`, %s, %s)'
) AS `sql`
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_SCHEMA = %s
AND DATA_TYPE IN ('char','varchar','text','mediumtext','longtext')",
$old_domain,
$new_domain,
DB_NAME
);
$columns = $wpdb->get_col( $columns_query );
foreach ( $columns as $column ) {
$wpdb->query( $column );
}
}
/**
* Register the admin menu
*/
function domain_replacer_admin_menu() {
add_menu_page(
'Domain Replacer',
'Domain Replacer',
'manage_options',
'domain-replacer',
'domain_replacer_admin_page'
);
}
add_action( 'admin_menu', 'domain_replacer_admin_menu' );
/**
* Render the admin menu page
*/
function domain_replacer_admin_page() {
// Check if the user has permission to run the function
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'You do not have sufficient permissions to access this page.', 'domain-replacer' ) );
}
// Check if the form has been submitted and the nonce is valid
if ( isset( $_POST['domain_replacer_submit'] ) && wp_verify_nonce( $_POST['domain_replacer_nonce'], 'domain_replacer' ) ) {
// Run the find_replace_domain_in_database() function
find_replace_domain_in_database( sanitize_text_field( $_POST['old_domain'] ), sanitize_text_field( $_POST['new_domain'] ) );
echo '<div class="notice notice-success is-dismissible"><p>' . esc_html__( 'The domain has been replaced.', 'domain-replacer' ) . '</p></div>';
}
?>
<div class="wrap">
<h1>Domain Replacer</h1>
<p>It’s crucial to back up your WordPress database before making any changes, including running the SQL query or using the PHP function provided in this post. Any mistakes or errors during the process can cause irreversible damage to your database, which could potentially result in the loss of data. To avoid any such issues, be sure to create a backup of your database, so you can restore it if anything goes wrong. You can use a plugin like UpdraftPlus or a similar backup tool to create a backup of your database and files. By taking this precautionary step, you can ensure that your data is safe and protected throughout the process of updating your domain in your WordPress database. </p>
<form method="post">
<?php wp_nonce_field('domain_replacer', 'domain_replacer_nonce'); ?>
<table class="form-table">
<tr>
<th scope="row"><label for="old_domain">Old Domain</label></th>
<td><input type="text" name="old_domain" id="old_domain" class="regular-text" value="" required></td>
</tr>
<tr>
<th scope="row"><label for="new_domain">New Domain</label></th>
<td><input type="text" name="new_domain" id="new_domain" class="regular-text" value="" required></td>
</tr>
</table>
<?php submit_button('Replace Domain', 'primary', 'domain_replacer_submit'); ?>
</form>
</div>
<?php
}
@devuri
Copy link
Author

devuri commented Feb 22, 2023

It’s crucial to back up your WordPress database before making any changes, including running the SQL query or using the PHP function provided in this post. Any mistakes or errors during the process can cause irreversible damage to your database, which could potentially result in the loss of data. To avoid any such issues, be sure to create a backup of your database, so you can restore it if anything goes wrong. You can use a plugin like UpdraftPlus or a similar backup tool to create a backup of your database and files. By taking this precautionary step, you can ensure that your data is safe and protected throughout the process of updating your domain in your WordPress database.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment