Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- add_action( 'init', function(){
- if( isset( $_GET['wpmudev-restore-attached-file-meta'] ) ){
- global $wpdb;
- $all_missing_images = get_posts(array(
- 'post_type' => 'attachment',
- 'posts_per_page' => -1,
- 'meta_key' => '_wp_attached_file',
- 'fields' => 'ids',
- 'meta_compare' => 'NOT EXISTS'
- ) );
- echo '<pre>';
- if( $all_missing_images ){
- echo 'total: '. count( $all_missing_images ) .'<br/>';
- foreach( $all_missing_images as $image_id ){
- $attachment_meta = get_post_meta( $image_id, '_wp_attachment_metadata', true );
- if( $attachment_meta && isset( $attachment_meta['file'] ) ){
- $file_path = $attachment_meta['file'];
- }else{
- @file_put_contents( dirname(__FILE__).'/missing-data.txt', ",". $image_id, FILE_APPEND );
- $guid = get_post_field( 'guid', $image_id, 'raw' );
- if( $guid && ( $pos = strpos( $guid, 'wp-content/uploads/') ) ){
- $file_path = substr( $guid, $pos + strlen('wp-content/uploads/') );
- }
- }
- if( isset( $file_path ) ){
- // d( $file_path );
- @file_put_contents( dirname(__FILE__).'/test.txt', ",". $image_id, FILE_APPEND );
- echo "Reverting file meta for image: {$image_id}\n";
- update_post_meta( $image_id, '_wp_attached_file', $file_path );
- }else{
- echo "Can't retrive the file path for this image ID: ". $image_id;
- }
- }
- }
- exit;
- }
- } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement