Skip to content

Instantly share code, notes, and snippets.

@CarlosLongarela
Last active December 1, 2018 15:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CarlosLongarela/681f6432cec79556471ba4d2ca2a3b86 to your computer and use it in GitHub Desktop.
Save CarlosLongarela/681f6432cec79556471ba4d2ca2a3b86 to your computer and use it in GitHub Desktop.
Script for change old WordPress url to New WordPress url, from dev to live, live to dev, changes for https and other uses
<?php
/**
* Script for change WordPress live url to local url,
* local url to live url, http url to https url
* or any other url change in database.
*
* @author Carlos Longarela <carlos@longarela.eu>
* @link https://tabernawp.com/
*
*/
define( 'SHORTINIT', true );
/** Absolute path to the WordPress directory. */
if ( ! defined( 'ABSPATH' ) ) {
define( 'ABSPATH', dirname( __FILE__ ) . '/' );
}
require_once ABSPATH . 'wp-load.php';
require ABSPATH . WPINC . '/formatting.php';
require ABSPATH . WPINC . '/kses.php';
function cl_change_wp_url( $old_url, $new_url ) {
global $wpdb;
$res = array();
$protocols = array( 'http', 'https' );
$cl_old_url = esc_url_raw( $old_url, $protocols );
$cl_new_url = esc_url_raw( $new_url, $protocols );
$res['error'] = false;
if ( empty( $cl_old_url ) ) {
$res['error'] = 'You must write a valid Old WordPress URL';
return $res;
} elseif ( empty( $cl_new_url ) ) {
$res['error'] = 'You must write a valid New WordPress URL';
return $res;
}
$res['sql_1'] = $wpdb->query( $wpdb->prepare(
"UPDATE {$wpdb->prefix}options SET option_value = replace( option_value, %s, %s ) WHERE option_name = 'home' OR option_name = 'siteurl'",
$cl_old_url,
$cl_new_url
) );
$res['sql_2'] = $wpdb->query( $wpdb->prepare(
"UPDATE {$wpdb->prefix}posts SET post_content = replace( post_content, %s, %s )",
$cl_old_url,
$cl_new_url
) );
$res['sql_3'] = $wpdb->query( $wpdb->prepare(
"UPDATE {$wpdb->prefix}postmeta SET meta_value = replace( meta_value, %s, %s )",
$cl_old_url,
$cl_new_url
) );
return $res;
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>WordPress URL Change</title>
<style>
h1 {
text-align: center;
}
.cl_wp_url_changer {
background-color: #000;
color: #ccc;
font-family: -apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Oxygen-Sans,Ubuntu,Cantarell,"Helvetica Neue",sans-serif;
font-size: 1.5em;
padding: 3em;
}
.cl_warning {
color: #f00;
}
.cl_wp_url_changer_form {
width: 80%;
text-align: center;
margin: auto;
border: 2px solid #ccc;
padding: 1em;
}
.cl_wp_url_changer_form label {
display: block;
margin-bottom: 1em;
}
.cl_wp_url_changer_form input[type="url"] {
font-size: 1em;
padding: 10px;
width: 20em;
}
.cl_wp_url_changer_form input[type="submit"] {
font-size: 1em;
padding: 1em 4em;
border-radius: 1em;
cursor: pointer;
font-weight: 600;
background-color: #247124;
color: #ccc;
border: 2px solid #ccc;
}
.cl_wp_url_changer_form input[type="submit"]:hover {
background-color: #7bdc7b;
color: #666;
border: 2px solid #666;
}
.cl_error, .cl_info {
width: 80%;
margin: 1em auto;
text-align: center;
padding: 0.2em 1em;
}
.cl_error {
border: 2px dotted #f00;
color: #f00;
}
.cl_info {
border: 2px dotted #247124;
color: #247124;
}
.cl_copy {
width: 80%;
margin: 1em auto;
padding: 1em;
text-align: right;
font-size: 70%;
font-style: italic;
}
.cl_copy a {
color: #ccc;
text-decoration: none;
border-bottom: 1px dotted #ccc;
}
.cl_copy a:hover {
color: #21759b;
text-decoration: none;
border-bottom: 1px dotted #21759b;
}
</style>
</head>
<body class="cl_wp_url_changer">
<h1><span class="cl_warning">WARNING !!!</span> Delete this file from your server when you'll finish to change urls</h1>
<?php
if ( ! empty( $_POST ) ) {
$res = cl_change_wp_url( $_POST['old_url'], $_POST['new_url'] );
if ( ! empty( $res['error'] ) ) {
echo '<div class="cl_error">' . $res['error'] . '</div>';
} else {
if ( false === $res['sql_1'] ) {
echo '<div class="cl_error">Error while executing query in table <strong><em>' . $wpdb->prefix . 'options</em></strong> !!!</div>';
} else {
echo '<div class="cl_info"><strong><em>' . $res['sql_1'] . '</em></strong> fields updated in table <strong><em>' . $wpdb->prefix . 'options</em></strong></div>';
}
if ( false === $res['sql_2'] ) {
echo '<div class="cl_error">Error while executing query in table <strong><em>' . $wpdb->prefix . 'posts</em></strong> !!!</div>';
} else {
echo '<div class="cl_info"><strong><em>' . $res['sql_1'] . '</em></strong> fields updated in table <strong><em>' . $wpdb->prefix . 'posts</em></strong></div>';
}
if ( false === $res['sql_3'] ) {
echo '<div class="cl_error">Error while executing query in table <strong><em>' . $wpdb->prefix . 'postmeta</em></strong> !!!</div>';
} else {
echo '<div class="cl_info"><strong><em>' . $res['sql_1'] . '</em></strong> fields updated in table <strong><em>' . $wpdb->prefix . 'postmeta</em></strong></div>';
}
}
}
?>
<form method="post" class="cl_wp_url_changer_form">
<label for="old_url">Old WordPress URL: <input type="url" name="old_url" id="old_url" value="" required></label>
<label for="new_url">New WordPress URL: <input type="url" name="new_url" id="new_url" value="" required></label>
<input type="submit" value="Change Old URL with New URL in WordPress database">
</form>
<div class="cl_copy">&copy; <a href="mailto:carlos@longarela.eu">Carlos Longarela</a> 2018 - <a href="https://tabernawp.com/">Taberna WordPress</a></div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment