Advertisement
kjm

migration_script

kjm
Feb 14th, 2014
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Revised version
  4.  *
  5.  * How to use:
  6.  *   1. Place this script in WordPress root directory.
  7.  *   2. Log in your WordPress admin dashborad.
  8.  *   3. put the address of this file in the Browser's address bar:
  9.  *      e.g. http://example.com/wordpress/this_file.php
  10.  *   4. Wait until 'Finished!' message is shown.
  11.  *
  12.  * This script will take the more time if you have the more uploaded files.
  13.  * So I recommend you to write the snippet below in your theme's functions.php
  14.  *
  15.  * function maintenace_mode() {
  16.  * if (!current_user_can('manage_options') && !is_user_logged_in())
  17.  *   wp_die('<h1>Maintenace Work in Progress</h1>');
  18.  * }
  19.  * add_action('get_header', 'maintenace_mode');
  20.  *
  21.  */
  22. if (current_user_can('manage_options' && !is_user_logged_in())) die();
  23.  
  24. require_once('./wp-config.php');
  25.  
  26. global $wpdb;
  27.  
  28. // STAGE 1: to rewrite _wp_attached_file values in postmeta table
  29. $key = '_wp_attached_file';
  30.  
  31. $wpdb->query("UPDATE $wpdb->postmeta SET meta_value=SUBSTRING(meta_value, 9) WHERE meta_key='{$key}'");
  32.  
  33. // STAGE 2: to rewrite guid value in posts table
  34. //          and _wp_attachment_metadata in postmeta table at a time
  35. $data = $wpdb->get_results("select ID, guid from $wpdb->posts WHERE post_type='attachment'");
  36. foreach ($data as $d) {
  37.     if (preg_match("/\\/wp-content\\/uploads\\/\\d{4}\\/\\d{2}\\/(.+)$/", $d->guid)) {
  38.         $new_guid = preg_replace("/\\d{4}\\/\\d{2}\\//", '', $d->guid);
  39.         $wpdb->query("UPDATE $wpdb->posts SET guid='{$new_guid}' WHERE ID='{$d->ID}'");
  40.         $meta_data = get_metadata('post', $d->ID, '_wp_attachment_metadata', true);
  41.         $unserialized_meta_data = maybe_unserialize($meta_data);
  42.         $new_meta_data = preg_replace("/\\d{4}\\/\\d{2}\\//", '', $unserialized_meta_data['file']);
  43.         $unserialized_meta_data['file'] = $new_meta_data;
  44.         $serialized_meta_data = maybe_serialize($unserialized_meta_data);
  45.         update_metadata('post', $d->ID, '_wp_attachment_metadata', $serialized_meta_data);
  46.     }
  47. }
  48. echo 'Finished!';
  49. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement