How to Generate and Download a CSV File of WordPress Pages

In this tutorial, you will learn how to create a custom function in WordPress that generates and downloads a CSV file containing information about your website's pages. This can be useful for exporting page data for analysis or backup purposes.

Prerequisites

  • Basic understanding of WordPress and PHP.

  • Access to your WordPress site's files via FTP or a file manager.

Step 1: Create a Custom Function

First, we'll create a custom function in your theme's functions.php file (or in a custom plugin if you prefer) to generate and download the CSV file.

// Add this code to your theme's functions.php file or a custom plugin

function generate_and_download_csv() {
    // Set the header for CSV file download
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="wordpress_pages.csv"');

    // Open output stream
    $output = fopen('php://output', 'w');

    // Write header row
    $header = array('ID', 'Title', 'Content', 'Date Published');
    fputcsv($output, $header);

    // Query to retrieve WordPress pages
    $pages_query = new WP_Query(array(
        'post_type' => 'page', // Retrieve pages
        'posts_per_page' => -1, // Retrieve all pages
        'orderby' => 'ID', // Order by post ID
        'order' => 'ASC' // Ascending order
    ));

    // Loop through the pages
    if ($pages_query->have_posts()) {
        while ($pages_query->have_posts()) {
            $pages_query->the_post();

            // Prepare data for the row
            $row = array(
                get_the_ID(), // Page ID
                get_the_title(), // Page title
                get_the_content(), // Page content
                get_the_date('Y-m-d H:i:s') // Date published
            );

            // Write the row to the CSV file
            fputcsv($output, $row);
        }
        wp_reset_postdata(); // Reset post data
    }

    // Close output stream
    fclose($output);

    // Terminate the script
    exit;
}

add_action('wp_ajax_generate_csv', 'generate_and_download_csv');

Step 2: Usage

<a href="#" id="generate-csv">Generate CSV</a>

<script>
jQuery(document).ready(function($) {
    $('#generate-csv').click(function(e) {
        e.preventDefault();
        $.ajax({
            url: '<?php echo admin_url('admin-ajax.php'); ?>',
            type: 'post',
            data: { action: 'generate_csv' },
            success: function(response) {
                // CSV has been generated and downloaded
            }
        });
    });
});
</script>

Step 3: Testing

  • Save or publish the page/post.

  • Visit the page/post in your web browser.

  • Click on the button/link you inserted to trigger the CSV generation and download process.

Conclusion

  • Once clicked, the custom function will generate a CSV file containing information about your WordPress pages.

  • The CSV file will be downloaded automatically, allowing you to access and use the data as needed.

Did you find this article valuable?

Support Tarik Omercehajic by becoming a sponsor. Any amount is appreciated!