-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathwp-migrate.php
More file actions
214 lines (198 loc) · 5.92 KB
/
wp-migrate.php
File metadata and controls
214 lines (198 loc) · 5.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
<?php
require_once('wp-load.php');
?>
<html>
<head>
<title>Migrate WP Site</title>
<style type="text/css">
body {
font-family:"Lucida Sans","Lucida Grande","Lucida Unicode","Lucida",sans-serif;
background-color: #666;
}
h1 {
font-size:24px;
margin:0;
padding:5px;
color:#fff;
text-shadow: 1px 1px rgba(0,0,0,0.2);
background-color: rgba(0,0,0,0.6);
-moz-border-radius:9px;
-webkit-border-radius:9px;
-khtml-border-radius:9px;
border-radius:9px;
}
h2 {
font-size: 18px;
}
#container {
width:600px;
margin:20px auto;
border:1px solid #333;
background-color: rgba(255,255,255,0.8);
-moz-border-radius:10px;
-webkit-border-radius:10px;
-khtml-border-radius:10px;
border-radius:10px;
}
#content {
padding:20px;
}
p {
font-size:14px;
line-height: 18px;
margin-bottom: 18px;
}
p:last-child {
margin-bottom: 0;
}
a, a:visited {
color:#20668a;
}
ul {
margin:10px;
padding: 0;
list-style: square;
}
form {}
label {
display: block;
font-weight:bold;
}
input[type=text] {
display: block;
-moz-border-radius:5px;
-webkit-border-radius:5px;
-khtml-border-radius:5px;
border-radius:5px;
width: 100%;
margin-bottom: 10px;
padding: 5px;
}
input[type=submit] {
color:#fff;
background: #20668a;
-moz-border-radius:5px;;
-webkit-border-radius:5px;
-khtml-border-radius:5px;
border-radius:5px;
text-shadow: 1px 1px #0b4462;
box-shadow: 1px 1px 3px rgba(0,0,0,0.5);
font-size:14px;
font-weight:bold;
position: relative;
cursor: pointer;
float:right;
margin:10px 0;
border:none;
}
input[type=submit]:active {
left:1px;
top:1px;
}
.message {
padding:5px;
margin:10px 0;
border-width: 2px;
border-style: solid;
list-style: none;
-moz-border-radius:5px;
-webkit-border-radius:5px;
-khtml-border-radius:5px;
border-radius:5px;
}
.message.success {
color:#30ac12;
border-color: #30ac12;
background-color: #afd9a5;
}
.message.warning {
border-color: #efce15;
color: #957236;
background-color: #f2e8af;
}
.message.error {
border-color: #c51414;
color: #c51414;
background-color: #f5a2a2;
}
</style>
</head>
<body>
<div id="container">
<h1>Migrate WordPress Site</h1>
<div id="content">
<?php
global $wpdb;
if ( !$_POST['submit_update_url'] ) { //FORM HAS NOT BEEN SUBMITTED
?>
<p class="message warning">This script will update all references in the DB to a new URL. Before proceeding, you should <a href="http://codex.wordpress.org/Backing_Up_Your_Database" target="_blank">make a complete backup of your database</a>.</p>
<ul>
<li>The current WP URL is: <strong><?php echo get_bloginfo('url'); ?></strong></li>
<li>Database: <?php echo DB_NAME; ?></li>
<li>DB Host: <?php echo DB_HOST; ?></li>
</ul>
<form action="#" method="post">
<label for="old_url">Current URL</label>
<input type="text" name="old_url" value="<?php echo get_bloginfo('url'); ?>" class="text" />
<label for="new_url">New URL</label>
<input type="text" name="new_url" class="text" />
<input type="submit" name="submit_update_url" value="Update Database" class="button " />
<p style="display:block;"> </p>
</form>
<?php
} else {
extract($_POST);
//CHECK FOR ERRORS
if ( !$old_url || !$new_url ) {
echo '<p class="message error">You must enter both the old and new URLs.</p>';
die();
}
//SHOW SQL ERRORS
$wpdb->show_errors();
echo '<h2>Updating Database...</h2>';
echo '<ul>';
$error = false;
$options_query = "UPDATE $wpdb->options SET option_value = replace(option_value, '$old_url', '$new_url') WHERE option_name = 'home' OR option_name = 'siteurl'";
if ( $wpdb->query($options_query) ) {
echo '<li class="message success">Options table updated successfully.</li>';
} else {
echo '<li class="message error">Problem updating options table.';
echo $wpdb->print_error().'</li>';
$error = true;
}
$posts_query = "UPDATE $wpdb->posts SET guid = replace(guid, '$old_url','$new_url')";
if ( $wpdb->query($posts_query) ) {
echo '<li class="message success">Posts table updated successfully.</li>';
} else {
echo '<li class="message error">Problem updating posts table. ';
echo $wpdb->print_error().'</li>';
$error = true;
}
$items_query = "UPDATE $wpdb->posts SET post_content = replace(post_content, '$old_url', '$new_url')";
if ( $wpdb->query($items_query) ) {
echo '<li class="message success">All posts/pages updated successfully.</li>';
} else {
echo '<li class="message error">Problem updating posts/pages. ';
echo $wpdb->print_error().'</li>';
$error = true;
}
$meta_query = "UPDATE $wpdb->postmeta SET meta_value = replace(meta_value, '$old_url', '$new_url')";
$meta_query = $wpdb->query($meta_query);
if ( $meta_query !== false ) {
echo '<li class="message success">All custom fields updated successfully. '.$meta_query.' fields were updated.</li>';
} else {
echo '<li class="message error">Problem updating custom fields. ';
echo $wpdb->print_error().'</li>';
$error = true;
}
if (!$error) {
echo '<p class="message success">All done. <a href="'.$new_url.'">Go to home page</a> | <a href="'.$new_url.'/wp-migrate.php">Do another migration</a></p>';
echo '<p class="message warning">You should delete the \'wp-migrate.php\' file from your root directory. Failing to do so is a major security risk and could cause your site to be hacked.</p>';
}
echo '</ul>';
}
?>
</div><!--#content -->
</div><!-- #container -->
</body>
</html>