Skip to content

Instantly share code, notes, and snippets.

@speeddragon
Last active October 16, 2015 09:48
Show Gist options
  • Save speeddragon/2d0ca42a3e2e70de1b4e to your computer and use it in GitHub Desktop.
Save speeddragon/2d0ca42a3e2e70de1b4e to your computer and use it in GitHub Desktop.
Change link from old domain to new domain link in Wordpress
<?php
# Change link from old domain to new domain in Wordpress
require_once('wp-config.php');
global $wpdb;
$newDomain = "newdomain.com";
$oldDomain = "olddomain.com";
# Posts
$query = "SELECT ID, post_content FROM wp_posts WHERE post_content LIKE '%" . $oldDomain . "%';";
$results = $wpdb->get_results($query);
foreach($results as $result) {
$text = str_replace($oldDomain, $newDomain, $result->post_content);
$wpdb->query(
$wpdb->prepare("UPDATE wp_posts SET post_content = %s WHERE ID = %d", $text, $result->ID)
);
}
# Options
$query = "SELECT option_id, option_value FROM wp_options WHERE option_value LIKE '%" . $oldDomain . "%';";
$results = $wpdb->get_results($query);
foreach($results as $result) {
$text = str_replace($oldDomain, $newDomain, $result->option_value);
$wpdb->query(
$wpdb->prepare("UPDATE wp_options SET option_value = %s WHERE option_id = %d", $text, $result->option_id)
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment