Changing wordpress domain at will

how nice would it be if we can change our wordpress installed domain anytime and anywhere?

The scenario is:

my production wordpress is blarblar.com and I want to duplicate a copy of it in dev.blarblar.com. I would import the db from production and run the following:

#!/bin/bash

if [ $# -ne 1 ]
then
echo ”
This simple script allows admin to quickly change the domain of wordpress multisite installs.
This gives you the ability to move wordpress installs anywhere anytime.

Usage: ./`basename $0` dev.yoursite.com

Version: 1.2
Author: Bernard Peh 9 july 2012

exit;
fi

# UPDATE YOUR SERVER CONFIGURATION HERE.
OLD_DOMAIN=yoursite.com
INSTALLED_DIR=/system_path/yoursite.com
NEW_DOMAIN=$1
PHP=/usr/bin/php

# quit if installed dir not found
if [ ! -d $INSTALLED_DIR ]; then
echo “WordPress installed directory not found. Please check the script config settings.”
exit 1
fi

# update all relevant file references to new domain
sed -i “s/=${OLD_DOMAIN}/=$NEW_DOMAIN/g” $INSTALLED_DIR/.htaccess
echo “updated root .htaccess”

sed -i “s/’${OLD_DOMAIN}’/’$NEW_DOMAIN’/g” $INSTALLED_DIR/wp-config.php
echo “updated wp-config.php”

# update w3tc folder
mv ${INSTALLED_DIR}/wp-content/w3-total-cache-config-${OLD_DOMAIN}.php \
${INSTALLED_DIR}/wp-content/w3-total-cache-config-${NEW_DOMAIN}.php
mv ${INSTALLED_DIR}/wp-content/w3tc-${OLD_DOMAIN} \
${INSTALLED_DIR}/wp-content/w3tc-${NEW_DOMAIN}
echo “renamed w3tc files and dirs”

# update w3tc minify
sed -i “s/-${OLD_DOMAIN}/-${NEW_DOMAIN}/g” $INSTALLED_DIR/wp-content/w3tc-${NEW_DOMAIN}/min/.htaccess
echo “updated w3tc minify .htaccess”

# update w3tc pgcache
sed -i “s/\\/${OLD_DOMAIN}\\//\\/${NEW_DOMAIN}\\//g” $INSTALLED_DIR/wp-content/w3tc-${NEW_DOMAIN}/pgcache/.htaccess
echo “updated w3tc pgcache .htaccess”

# update db references to new domain
DB_USER=`cat $INSTALLED_DIR/wp-config.php | grep DB_USER | awk -F\’ ‘{print $4}’`
DB_PASSWORD=`cat $INSTALLED_DIR/wp-config.php | grep DB_PASSWORD | awk -F\’ ‘{print $4}’`
DB_NAME=`cat $INSTALLED_DIR/wp-config.php | grep DB_NAME | awk -F\’ ‘{print $4}’`
DB_HOST=`cat $INSTALLED_DIR/wp-config.php | grep DB_HOST | awk -F\’ ‘{print $4}’`

# make sure we don’t run this multiple times. check current entry is correct before running
VAL=`mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-e “select meta_value from wp_sitemeta where meta_key=’siteurl’ and meta_value like ‘%$NEW_DOMAIN%'” \
| tail -n 1`
if [[ $VAL == *$NEW_DOMAIN* ]]; then
echo “You ran this script before. Please do not run it again.”
exit 1
else
echo “updating the db tables…”
fi

mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-e “update wp_blogs set domain=’$NEW_DOMAIN’ where domain=’$OLD_DOMAIN’;”
echo “wp_blogs table updated”

mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-e “update wp_site set domain=’$NEW_DOMAIN’ where domain=’$OLD_DOMAIN’;”
echo “wp_site table updated”

mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-e “update wp_sitemeta set meta_value=replace(meta_value, ‘$OLD_DOMAIN’, ‘$NEW_DOMAIN’) \
where meta_value like ‘%$OLD_DOMAIN%’;”
echo “wp_sitemeta updated”

NUM_OF_BLOGS=`mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-e “select count(*) from wp_blogs” | tail -n 1`
echo “–> found $NUM_OF_BLOGS wordpress site… updating the relevant tables”

for (( i=1; i<$NUM_OF_BLOGS+1; i++)) do
if [ $i == 1 ]; then
TABLE=wp_options
else
TABLE=wp_${i}_options
fi

mysql –user=$DB_USER –password=$DB_PASSWORD –database=$DB_NAME –host=$DB_HOST \
-B -e “select option_id, option_value from $TABLE where option_value like ‘%${OLD_DOMAIN}%'” \
–column-names=false | while read k v; do \
echo $v | $PHP -R ‘
$link = mysql_connect(“‘$DB_HOST'”, “‘$DB_USER'”, “‘$DB_PASSWORD'”);
mysql_select_db(“‘$DB_NAME'”,$link);
$a = “”;
$a = @unserialize($argn);
// if serialized data, update them and store in mysql
if ($a !== false) {
$a = serialize(preg_replace_array(“/’$OLD_DOMAIN’/”, “‘$NEW_DOMAIN'”, $a));
$link = mysql_connect(“‘$DB_HOST'”, “‘$DB_USER'”, “‘$DB_PASSWORD'”);
mysql_select_db(“‘$DB_NAME'”,$link);
$sql = “update ‘$TABLE’ set option_value=\””.mysql_real_escape_string($a).”\” where option_id=’$k'”;
mysql_query($sql, $link) or die(“cannot update db”);
}
else {
$sql = “select option_value from ‘$TABLE’ where option_id=’$k'”;
$value = mysql_result(mysql_query($sql, $link),0) or die(“cannot select”);
if (!preg_match(“/’$NEW_DOMAIN’/”, $value)) {
$sql = “update ‘$TABLE’ set option_value=replace(option_value, \”‘$OLD_DOMAIN’\”, \”‘$NEW_DOMAIN’\”) where option_value like \”‘%$OLD_DOMAIN%’\””;
mysql_query($sql, $link) or die(“cannot update db”);
}
}
function preg_replace_array($pattern, $replacement, $subject, $limit=-1) {
if (is_array($subject)) {
foreach ($subject as &$value) {
$value=preg_replace_array($pattern, $replacement, $value, $limit);
}
return $subject;
}
else {
return preg_replace($pattern, $replacement, $subject, $limit);
}
}
‘; done;
done;
echo “wp_options tables updated”

echo “flushed memcached just incase wordpress is using memcache”
echo “flush_all” | nc localhost 11211
echo -e “\nDID YOU EXPECT IT TO BE HARDER? I’ve taken all sufferings from you!”

 

Author: bpeh

Bernard Peh is a great passioner of web technologies and one of the co-founder of Sitecritic.net Website Design and Reviews. He works with experienced web designers and developers everyday, developing and designing commercial websites. He specialises mainly in SEO and PHP programming.