Menu export: a client’s request sparks a WordPress function!

Recently, a client faced a common yet intricate challenge – exporting multiple WordPress menus effortlessly.

Recently, a client faced a common yet intricate challenge – exporting multiple WordPress menus effortlessly. With 8 menus ranging from 10 to 100 items, the task demanded a solution beyond manual copy-pasting. Recognizing the need for efficiency, the client approached us for a tailored function to streamline the process.

Understanding the client’s internal needs, we crafted a specialized WordPress function that exports menu titles and links into a CSV file. This solution not only eliminated the tedious manual effort but also provided a scalable approach for future menu management. The function, designed to handle various menu sizes, ensures a seamless export experience.

function export_menu_to_csv() {
    $menu_name = 'your-menu-slug'; // Replace with your actual menu slug

    $menu_items = wp_get_nav_menu_items($menu_name);

    if ($menu_items) {
        $csv_output = "Title,Link\n";

        foreach ($menu_items as $item) {
            $csv_output .= '"' . $item->title . '","' . $item->url . "\"\n";
        }

        // Output CSV headers
        header("Content-type: text/csv");
        header("Content-disposition: attachment; filename=menu_export.csv");

        // Output the CSV data
        echo $csv_output;

        // Terminate WordPress execution
        exit();
    }
}

// Add an action hook to trigger the export (you can remove it after use)
add_action('init', 'export_menu_to_csv');

Feel free to adjust the content to match the specifics of your client’s situation and the unique features of the WordPress function you implemented.

Nikola Stulic

Father of two beautiful girls. WordPress developer trapped inside the mind of a math teacher (and vice versa). Often doodles the Greek letter Xi. Loves to wear suits and ties.

WordPress contributor, educator, translator. WordCamp Europe organizer.

Add comment

Follow us

Don't be shy, get in touch. We love meeting interesting people and making new friends.