Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save vijayhardaha/25d09e2a6db323eb228ce3f4b0a14529 to your computer and use it in GitHub Desktop.
Save vijayhardaha/25d09e2a6db323eb228ce3f4b0a14529 to your computer and use it in GitHub Desktop.
Exporting WordPress Media Library Images: A Custom PHP Solution

Download/Export All WordPress Media Library Images

Managing a WordPress media library with numerous images can be a daunting task. Sometimes, you may need to export all these images for various purposes, such as creating backups or performing bulk image operations. This custom PHP code allows you to export all the images in your WordPress media library to a CSV file for easy access and management.

How to Use the Code

Follow these steps to use the provided custom PHP code to export your WordPress media library images:

  1. Open your theme's functions.php file.

  2. Copy and paste the following code snippet into the functions.php file:

/**
 * Export all the WordPress website media images.
 *
 * This custom code exports all the media library images
 * into a CSV file.
 *
 * How to use:
 * Put this custom code into your theme's functions.php file
 * and access the website home page with the ?magic-export parameter.
 * Example: https://yourwebsite.com?magic-export
 *
 * The exported-images.csv file will be generated in your website's root folder,
 * which you can access via a similar URL:
 * https://yourwebsite.com/exported-images.csv
 */
function vkh_custom_export_all_the_images() {
    // Check for 'magic-export' parameter in the URL to trigger the export.
    if ( isset( $_GET['magic-export'] ) ) {
        // Fetch all the images via a query.
        $images = get_posts(
            array(
                'post_type'      => 'attachment',
                'post_mime_type' => 'image',
                'posts_per_page' => -1,
                'post_status'    => 'any',
            )
        );

        // Check if images are found.
        if ( $images ) {
            // Open a CSV file.
            $fp = fopen( ABSPATH . 'exported-images.csv', 'w' );

            // Define the CSV headers.
            $header_fields = array( 'ID', 'Title', 'URL', 'Original URL' );

            // Put the CSV headers into the file.
            fputcsv( $fp, $header_fields );

            // Loop through the found images.
            foreach ( $images as $image ) {
                // Get image title.
                $title = get_the_title( $image->ID );

                // Get image original URL.
                $orig_url = wp_get_original_image_url( $image->ID );

                // Get attachment URL.
                $url = wp_get_attachment_url( $image->ID );

                // Prepare CSV row data.
                $fields = array( $image->ID, $title, $url, $orig_url );

                // Put the CSV row data into the file.
                fputcsv( $fp, $fields );
            }

            // Close the file.
            fclose( $fp );
        }
    }
}
// Call the function on the 'init' action.
add_action( 'init', 'vkh_custom_export_all_the_images' );
  1. Save the changes to your functions.php file.

  2. Access your WordPress website's home page with the ?magic-export parameter. For example: https://yourwebsite.com?magic-export

  3. The code will generate a CSV file named exported-images.csv in your website's root folder. You can access this file using the following URL: https://yourwebsite.com/exported-images.csv

Conclusion

This custom PHP code simplifies the process of exporting all the images in your WordPress media library to a CSV file. It can be a valuable tool for managing and organizing your media assets more effectively.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment