Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Revised version
- *
- * How to use:
- * 1. Place this script in WordPress root directory.
- * 2. Log in your WordPress admin dashborad.
- * 3. put the address of this file in the Browser's address bar:
- * e.g. http://example.com/wordpress/this_file.php
- * 4. Wait until 'Finished!' message is shown.
- *
- * This script will take the more time if you have the more uploaded files.
- * So I recommend you to write the snippet below in your theme's functions.php
- *
- * function maintenace_mode() {
- * if (!current_user_can('manage_options') && !is_user_logged_in())
- * wp_die('<h1>Maintenace Work in Progress</h1>');
- * }
- * add_action('get_header', 'maintenace_mode');
- *
- */
- if (current_user_can('manage_options' && !is_user_logged_in())) die();
- require_once('./wp-config.php');
- global $wpdb;
- // STAGE 1: to rewrite _wp_attached_file values in postmeta table
- $key = '_wp_attached_file';
- $wpdb->query("UPDATE $wpdb->postmeta SET meta_value=SUBSTRING(meta_value, 9) WHERE meta_key='{$key}'");
- // STAGE 2: to rewrite guid value in posts table
- // and _wp_attachment_metadata in postmeta table at a time
- $data = $wpdb->get_results("select ID, guid from $wpdb->posts WHERE post_type='attachment'");
- foreach ($data as $d) {
- if (preg_match("/\\/wp-content\\/uploads\\/\\d{4}\\/\\d{2}\\/(.+)$/", $d->guid)) {
- $new_guid = preg_replace("/\\d{4}\\/\\d{2}\\//", '', $d->guid);
- $wpdb->query("UPDATE $wpdb->posts SET guid='{$new_guid}' WHERE ID='{$d->ID}'");
- $meta_data = get_metadata('post', $d->ID, '_wp_attachment_metadata', true);
- $unserialized_meta_data = maybe_unserialize($meta_data);
- $new_meta_data = preg_replace("/\\d{4}\\/\\d{2}\\//", '', $unserialized_meta_data['file']);
- $unserialized_meta_data['file'] = $new_meta_data;
- $serialized_meta_data = maybe_serialize($unserialized_meta_data);
- update_metadata('post', $d->ID, '_wp_attachment_metadata', $serialized_meta_data);
- }
- }
- echo 'Finished!';
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement