Last active
October 16, 2015 09:48
-
-
Save speeddragon/2d0ca42a3e2e70de1b4e to your computer and use it in GitHub Desktop.
Change link from old domain to new domain link in Wordpress
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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