[wp-trac] [WordPress Trac] #27804: bug when updating after domain change settings

WordPress Trac noreply at wordpress.org
Tue Apr 15 08:13:39 UTC 2014


#27804: bug when updating after domain change settings
-------------------------------+------------------------------
 Reporter:  robomotic          |       Owner:
     Type:  defect (bug)       |      Status:  new
 Priority:  normal             |   Milestone:  Awaiting Review
Component:  Administration     |     Version:  3.8.2
 Severity:  normal             |  Resolution:
 Keywords:  reporter-feedback  |     Focuses:
-------------------------------+------------------------------
Changes (by Denis-de-Bernardy):

 * keywords:   => reporter-feedback


Comment:

 It's not very clear what you're meaning or highlighting in the ticket.

 To be very honest though, WordPress isn't really designed to allow to
 change the site and WP URLs. There admittedly is UI (and even two defines)
 to do so but, frankly, changing either or both have been a can of worms
 for as long as I can remember. Key problems:

 - Changing the WP URL is likely to log you out
 - URLs in existing posts don't change when the site URL is updated
 - Using www. in one URL but not the other can lock you out of the admin
 area due to infinite redirects

 And don't even get me started on multisite-related problems, WP in its
 subfolder-related problems, and yada yada.

 Below is a quick and dirty script I recently used to migrate a client's
 site from using www3 to www, to give you an idea of the kind of mess it
 can be. And it only began to scratch the surface, as I didn't want to
 clean up the other tables.

 {{{
 #!/usr/bin/env php
 <?php
 function override_plugins($plugins) {
     return array();
 }
 function override_theme($theme) {
     return 'notheme';
 }
 $wp_filter = array();
 $wp_filter['option_active_plugins'][PHP_INT_MAX]['override_plugins'] =
 array(
     'function'      => 'override_plugins',
     'accepted_args' => 1,
 );
 $wp_filter['template'][PHP_INT_MAX]['override_theme'] = array(
     'function'      => 'override_theme',
     'accepted_args' => 1,
 );
 $wp_filter['stylesheet'][PHP_INT_MAX]['override_theme'] = array(
     'function'      => 'override_theme',
     'accepted_args' => 1,
 );
 require_once __DIR__.'/../www/wp-load.php';
 require_once __DIR__.'/../www/wp-admin/includes/post.php';


 #
 # Catch all errors
 #
 function exception_error_handler($errno, $errstr, $errfile, $errline ) {
     throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
 }
 set_error_handler("exception_error_handler");


 #
 # www3 converter
 #
 function convert_www3_to_www($value) {
     if (is_string($value)) {
         $value = str_replace('www3.example.com', 'www.example.com',
 $value);
     }
     elseif (is_array($value)) {
         foreach ($value as $key => $val) {
             $value[$key] = convert_www3_to_www($val);
         }
     }
     elseif (is_object($value)) {
         foreach (get_object_vars($value) as $key) {
             $value->$key = convert_www3_to_www($value->$key);
         }
     }
     return $value;
 }

 #
 # Clean up Lee's garbage
 #
 $query = <<<EOS
 SELECT  option_name
 FROM    $wpdb->options
 WHERE   option_value LIKE '%Users/lee/Sites%'
 OR      option_value LIKE '%local.example.com%'
 EOS;
 $refs = $wpdb->get_col($query);

 foreach ($refs as $name) {
     echo "delete option $name\n";
     delete_option($name);
 }


 #
 # Convert www3 to www in options
 #
 $query = <<<EOS
 SELECT  option_name
 FROM    $wpdb->options
 WHERE   option_value LIKE '%www3.example.com%'
 EOS;
 $refs = $wpdb->get_col($query);

 foreach ($refs as $name) {
     $value = get_option($name);
     $value = convert_www3_to_www($value);
     echo "update option $name\n";
     update_option($name, $value);
 }


 #
 # Convert www3 to www in posts
 #
 $query = <<<EOS
 SELECT  ID
 FROM    $wpdb->posts
 WHERE   post_content LIKE '%www3.example.com%'
 OR      post_excerpt LIKE '%www3.example.com%'
 OR      post_title   LIKE '%www3.example.com%'
 EOS;
 $refs = $wpdb->get_col($query);

 foreach($refs as $id) {
     $post = get_post($id);
     $post->post_title   = convert_www3_to_www($post->post_title);
     $post->post_content = convert_www3_to_www($post->post_content);
     $post->post_excerpt = convert_www3_to_www($post->post_excerpt);
     echo "update post $id\n";
     wp_update_post($post);
 }


 #
 # Convert www3 to www in postmeta
 #
 $query = <<<EOS
 SELECT  post_id, meta_key, count(distinct post_id) = 1 as single
 FROM    $wpdb->postmeta
 WHERE   meta_value LIKE '%www3.example.com%'
 GROUP BY meta_key
 EOS;
 $refs = $wpdb->get_results($query);

 foreach ($refs as $ref) {
     $value = get_post_meta($ref->post_id, $ref->meta_key, (bool)
 $ref->single);
     $value = convert_www3_to_www($value);
     echo "update post meta $ref->post_id $ref->meta_key\n";
     update_post_meta($ref->post_id, $ref->meta_key, $value);
 }
 }}}

--
Ticket URL: <https://core.trac.wordpress.org/ticket/27804#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list