Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Search & Replace SQL Generator for changing WordPress URLs
<div>Old Url:</div> http://<input id="old_url" name="old_url" type="text" value="old_url" onchange="replaceMyText()" onkeypress="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();"/>
<div>New Url:</div> http://<input id="new_url" name="new_url" type="text" value="new_url" onchange="replaceMyText()" onkeypress="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();"/>
<div id="haystack">
UPDATE wp_options SET option_value = replace(option_value, 'http://old_url', 'http://new_url') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://old_url','http://new_url');
UPDATE wp_posts SET post_content = replace(post_content, 'http://old_url', 'http://new_url');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://old_url','http://new_url');
UPDATE wp_options SET option_value = replace(option_value, 'http://www.old_url', 'http://new_url') WHERE option_name = 'home' OR option_name = 'siteurl';
UPDATE wp_posts SET guid = replace(guid, 'http://www.old_url','http://new_url');
UPDATE wp_posts SET post_content = replace(post_content, 'http://www.old_url', 'http://new_url');
UPDATE wp_postmeta SET meta_value = replace(meta_value,'http://www.old_url','http://new_url');
</div>
<script type='text/javascript'>
var sqlText = document.getElementById("haystack").innerHTML;
function replaceMyText(haystackText = sqlText) {
var old_url = document.getElementById('old_url').value;
var new_url = document.getElementById('new_url').value;
var old_default = new RegExp('old_url', "ig");
var new_default = new RegExp('new_url', "ig");
var replaced = "";
if (new_url.length > 0 && old_url.length > 0) {
replaced = haystackText.replace(old_default, old_url).replace(new_default, new_url);
document.getElementById("haystack").innerHTML = replaced;
SyntaxHighlighter.highlight();
}
}
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment