function fix_image_paths_callback() { global $wpdb; $backup_base = 'https://darkcyan-frog-694249.hostingersite.com/wp-content/uploads/'; $per_page = 10; // Pagination logic $paged = isset($_GET['paged']) ? max(1, intval($_GET['paged'])) : 1; $offset = ($paged - 1) * $per_page; // Total count for progress/pagination $total_posts = $wpdb->get_var("SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish'"); $total_pages = ceil($total_posts / $per_page); // Fetch this batch $posts = $wpdb->get_results($wpdb->prepare(" SELECT ID, post_content FROM {$wpdb->posts} WHERE post_type='post' AND post_status='publish' ORDER BY ID ASC LIMIT %d OFFSET %d ", $per_page, $offset)); echo "

Image Path Fixer + Downloader (Batch Mode)

"; echo "

Batch {$paged} of {$total_pages} ({$per_page} posts per batch, {$total_posts} total posts)

"; echo "
"; echo ""; echo "Jump to batch #: "; echo ""; echo "
"; if(empty($posts)){ echo "

No posts found in this batch.

"; return; } $filename_log = []; foreach($posts as $post){ echo "

Post ID {$post->ID}

"; $orig = $post->post_content; $upd = $orig; preg_match_all('/]+src=[\'"]([^\'"]+)[\'"]/i',$orig,$m); if(empty($m[1])){ echo "

No images.

"; continue; } foreach($m[1] as $url){ $path = parse_url($url, PHP_URL_PATH); if(strpos($path,'/uploads/')===false) continue; $file = basename($path); // year/month if(preg_match('#/uploads/(\d{4})/(\d{2})/#',$path,$d)){ list(,$year,$month) = $d; $orig_filename = $file; $orig_folder = "{$year}/{$month}/"; } elseif(preg_match('#/uploads/(\d{4})-(\d{2})-#',$path,$d2)){ list(,$year,$month) = $d2; $orig_filename = preg_replace('/^(?:'.$year.'-'.$month.'-)+/', '', $file); $orig_folder = "{$year}/{$month}/"; } else { continue; } $base = pathinfo($file, PATHINFO_FILENAME); $ext = pathinfo($file, PATHINFO_EXTENSION); $prefix = "{$year}-{$month}-"; $pattern = '/^(?:' . preg_quote($prefix,'/') . '){2,}/'; $base = preg_replace($pattern, $prefix, $base); if(strpos($base, $prefix) === 0){ $search = $base; } else { $search = $prefix . $base; } $like = "%/uploads/{$year}/{$month}/{$search}%." . $ext; $att = $wpdb->get_var($wpdb->prepare(" SELECT ID FROM {$wpdb->posts} WHERE post_type='attachment' AND post_mime_type LIKE 'image%%' AND guid LIKE %s LIMIT 1 ", $like )); if($att && ($fp = get_attached_file($att)) && file_exists($fp)){ $real = basename($fp); $new = "/wp-content/uploads/{$real}"; $upd = str_replace($url,$new,$upd); $filename_log[] = [ 'old' => $file, 'new' => $real, 'status' => 'found' ]; continue; } // Check if file is in uploads (not in media library) $upload_dir = wp_upload_dir(); $new_filename = $search . '.' . $ext; $new_path = trailingslashit($upload_dir['path']) . $new_filename; if(file_exists($new_path)){ $filename_log[] = [ 'old' => $file, 'new' => $new_filename, 'status' => 'file_exists_but_not_registered' ]; // Optionally: register as attachment here. continue; } // Try to download from backup $download_url = $backup_base . $orig_folder . $orig_filename; require_once(ABSPATH . 'wp-admin/includes/file.php'); require_once(ABSPATH . 'wp-admin/includes/media.php'); require_once(ABSPATH . 'wp-admin/includes/image.php'); $tmp = download_url($download_url, 10); if( is_wp_error($tmp) ) { $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'download_failed' ]; continue; } if(!@rename($tmp, $new_path)){ @unlink($tmp); $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'move_failed' ]; continue; } $file_array = [ 'name' => $new_filename, 'tmp_name' => $new_path ]; $attach_id = media_handle_sideload($file_array, $post->ID); if(is_wp_error($attach_id)){ @unlink($new_path); $filename_log[] = [ 'old' => $file, 'new' => '', 'status' => 'media_failed' ]; continue; } $real = basename(get_attached_file($attach_id)); $new = "/wp-content/uploads/{$real}"; $upd = str_replace($url, $new, $upd); $filename_log[] = [ 'old' => $file, 'new' => $real, 'status' => 'downloaded' ]; } if($upd !== $orig){ wp_update_post(['ID'=>$post->ID,'post_content'=>$upd]); echo "

💾 Updated post.

"; } else { echo "

🟢 No changes.

"; } } // Show log and pagination controls if(!empty($filename_log)){ echo "

Filename Log (this batch)

"; foreach($filename_log as $row){ echo ""; } echo "
OldNewStatus
{$row['old']}{$row['new']}{$row['status']}
"; } // Pagination navigation echo "
"; if($paged > 1){ $prev = $paged - 1; echo "« Previous batch "; } if($paged < $total_pages){ $next = $paged + 1; echo "Next batch »"; } echo "
"; echo ""; } Page not found - Keliones su Vaikais

Puslapis nerastas

The link you followed may be broken, or the page may have been removed.