Advertisement
ppratham00

smush-missing-meta

Oct 23rd, 2020
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.37 KB | None | 0 0
  1. <?php
  2. add_action( 'init', function(){
  3.     if( isset( $_GET['wpmudev-restore-attached-file-meta'] ) ){
  4.         global $wpdb;
  5.         $all_missing_images = get_posts(array(
  6.             'post_type' => 'attachment',
  7.             'posts_per_page' => -1,
  8.             'meta_key' => '_wp_attached_file',
  9.             'fields' => 'ids',
  10.             'meta_compare' => 'NOT EXISTS'
  11.         ) );
  12.         echo '<pre>';
  13.         if( $all_missing_images ){
  14.             echo 'total: '. count( $all_missing_images ) .'<br/>';
  15.             foreach( $all_missing_images as $image_id ){
  16.                 $attachment_meta = get_post_meta( $image_id, '_wp_attachment_metadata', true );
  17.                 if( $attachment_meta && isset( $attachment_meta['file'] ) ){
  18.                     $file_path = $attachment_meta['file'];
  19.                 }else{
  20.                     @file_put_contents( dirname(__FILE__).'/missing-data.txt', ",". $image_id, FILE_APPEND );
  21.                     $guid = get_post_field( 'guid', $image_id, 'raw' );
  22.                     if( $guid && ( $pos = strpos( $guid, 'wp-content/uploads/') ) ){
  23.                         $file_path = substr( $guid, $pos + strlen('wp-content/uploads/') );
  24.                     }
  25.                 }
  26.                 if( isset( $file_path ) ){
  27.                     // d( $file_path );
  28.                     @file_put_contents( dirname(__FILE__).'/test.txt', ",". $image_id, FILE_APPEND );
  29.                     echo "Reverting file meta for image: {$image_id}\n";
  30.                     update_post_meta( $image_id, '_wp_attached_file', $file_path );
  31.                 }else{
  32.                     echo "Can't retrive the file path for this image ID: ". $image_id;
  33.                 }
  34.             }
  35.         }
  36.         exit;
  37.     }
  38. } );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement