function oic_get_orphan_images() { global $wpdb; $used_images = []; // Buscar imágenes usadas en posts, productos, páginas $posts = $wpdb->get_col("SELECT post_content FROM {$wpdb->posts} WHERE post_type IN ('post','page','product') AND post_status='publish'"); foreach ($posts as $content) { preg_match_all('/wp-content\/uploads\/[^"\']+/', $content, $matches); $used_images = array_merge($used_images, $matches[0]); } // Obtener todas las imágenes adjuntas $attachments = get_posts([ 'post_type' => 'attachment', 'post_mime_type' => 'image', 'numberposts' => -1, 'post_status' => 'inherit', ]); $orphans = []; foreach ($attachments as $attachment) { $url = wp_get_attachment_url($attachment->ID); if (!in_array(parse_url($url, PHP_URL_PATH), $used_images)) { $orphans[] = $attachment; } } return $orphans; } add_action('admin_menu', function() { add_menu_page( __('Orphan Images', 'orphan-image-cleaner'), __('Orphan Images', 'orphan-image-cleaner'), 'manage_options', 'orphan-image-cleaner', 'oic_admin_page', 'dashicons-trash', 80 ); }); function oic_admin_page() { $orphans = oic_get_orphan_images(); echo '
' . __('Images deleted.', 'orphan-image-cleaner') . '