Useful Plugin to List all Images

List all images

This plugin for Wordpress will list all images used in the site and give you the page name and the images location.

I used this to ensure all the images were of the correct format and moving only the required images to the new website.

You have the option to download the list to a csv to sort the listing

Exported Listing
List all Images Plugin php Code
<?php
/*
Plugin Name: List All Images in Posts, Pages & Sliders (With Page Name)
Plugin URI: https://yourwebsite.com
Description: Lists all images used in posts, pages, custom post types (like sliders), and exports them with the page/post title.
Version: 1.6
Author: Anything Access
Author URI: https://anythingaccess.com
License: GPL2
*/

if (!defined('ABSPATH')) {
    exit; // Exit if accessed directly
}

// Add menu page in the admin dashboard
function lai_add_admin_menu() {
    add_management_page('List All Images', 'List All Images', 'manage_options', 'list-all-images', 'lai_display_images');
}
add_action('admin_menu', 'lai_add_admin_menu');

// Function to get all images with their page/post title
function lai_get_all_images() {
    global $wpdb;

    // Get all public post types (including sliders, if any)
    $post_types = get_post_types(['public' => true], 'names');

    if (empty($post_types)) {
        return [];
    }

    // Query all published posts/pages/custom post types
    $placeholders = implode(',', array_fill(0, count($post_types), '%s'));
    $query = $wpdb->prepare(
        "SELECT ID, post_title, post_content FROM $wpdb->posts WHERE post_type IN ($placeholders) AND post_status = 'publish'",
        $post_types
    );

    $results = $wpdb->get_results($query);
    $images = [];

    foreach ($results as $post) {
        // Extract images from post content
        preg_match_all('/<img[^>]+src=["\']([^"\']+)["\']/i', $post->post_content, $matches);
        if (!empty($matches[1])) {
            foreach ($matches[1] as $img) {
                $images[] = [
                    'title' => esc_html($post->post_title), 
                    'url' => esc_url($img)
                ];
            }
        }

        // Extract images from meta fields (for sliders)
        $meta_images = lai_get_images_from_meta($post->ID, $post->post_title);
        $images = array_merge($images, $meta_images);
    }

    return $images;
}

// Function to extract images from meta fields (for sliders, galleries, etc.)
function lai_get_images_from_meta($post_id, $post_title) {
    $images = [];
    $meta_values = get_post_meta($post_id);

    if (!is_array($meta_values) || empty($meta_values)) {
        return [];
    }

    foreach ($meta_values as $key => $value) {
        if (is_array($value) && isset($value[0])) {
            $data = maybe_unserialize($value[0]);

            if (is_string($data) && filter_var($data, FILTER_VALIDATE_URL)) {
                $images[] = [
                    'title' => esc_html($post_title), 
                    'url' => esc_url($data)
                ];
            } elseif (is_array($data)) {
                foreach ($data as $item) {
                    if (is_string($item) && filter_var($item, FILTER_VALIDATE_URL)) {
                        $images[] = [
                            'title' => esc_html($post_title), 
                            'url' => esc_url($item)
                        ];
                    }
                }
            }
        }
    }

    return $images;
}

// Function to export images with page titles to CSV
function lai_export_csv() {
    if (isset($_GET['export']) && $_GET['export'] === 'csv') {
        $images = lai_get_all_images();

        if (!empty($images)) {
            header('Content-Type: text/csv');
            header('Content-Disposition: attachment; filename="images-list.csv"');
            
            $output = fopen('php://output', 'w');
            fputcsv($output, ['Page/Post Title', 'Image URL']); // CSV header
            
            foreach ($images as $image) {
                fputcsv($output, [$image['title'], $image['url']]);
            }

            fclose($output);
            exit;
        } else {
            wp_die('No images found to export.');
        }
    }
}
add_action('admin_init', 'lai_export_csv');

// Display the images with titles on the admin page
function lai_display_images() {
    echo '<div class="wrap">';
    echo '<h1>List of All Images Used in Posts, Pages & Sliders</h1>';
    
    $images = lai_get_all_images();

    if (!empty($images)) {
        echo '<a href="' . esc_url(admin_url('tools.php?page=list-all-images&export=csv')) . '" class="button button-primary">Download CSV</a>';
        echo '<table class="widefat" style="margin-top: 20px;">';
        echo '<thead><tr><th>Page/Post Title</th><th>Image URL</th></tr></thead><tbody>';
        
        foreach ($images as $image) {
            echo '<tr>';
            echo '<td>' . esc_html($image['title']) . '</td>';
            echo '<td><a href="' . esc_url($image['url']) . '" target="_blank">' . esc_url($image['url']) . '</a></td>';
            echo '</tr>';
        }

        echo '</tbody></table>';
    } else {
        echo '<p>No images found.</p>';
    }

    echo '</div>';
}

List of other wordpress code samples or plugins

Or you can a plugin which may do this and more , I havent tried this plugin as the above code was simple and free, but it may offer you more functionality.