Skip to main content
Fixed formatting
Source Link
fuxia
  • 107.1k
  • 38
  • 255
  • 459

// Add export/import options to admin menu function add_custom_post_type_export_import_menu() { add_submenu_page( 'edit.php?post_type=fr-teams', 'Export/Import', 'Export/Import', 'manage_options', 'custom_post_type_export_import', 'custom_post_type_export_import_page' ); } add_action('admin_menu', 'add_custom_post_type_export_import_menu');

// Render export/import page content function custom_post_type_export_import_page() { ?>

Custom Post Type Export/Import

// Add export/import options to admin menu
function add_custom_post_type_export_import_menu() {
    add_submenu_page(
        'edit.php?post_type=fr-teams',
        'Export/Import',
        'Export/Import',
        'manage_options',
        'custom_post_type_export_import',
        'custom_post_type_export_import_page'
    );
}
add_action('admin_menu', 'add_custom_post_type_export_import_menu');


// Render export/import page content
function custom_post_type_export_import_page() {
    ?>
    <div class="wrap">
        <h1>Custom Post Type Export/Import</h1>
        
        <h2>Export</h2>
        <form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
        <input type="hidden" name="action" value="custom_post_type_export">
        <?php wp_nonce_field('custom_post_type_export'); ?>
        <p>
            <input type="submit" class="button button-primary" value="Export Custom Post Type">
        </p>
    </form>
    
    <h2>Import</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="custom_post_type_import">
        <?php wp_nonce_field('custom_post_type_import'); ?>
        <p>
            <input type="file" name="import_file">
        </p>
        <p>
            <input type="submit" class="button button-primary" value="Import Custom Post Type">
        </p>
    </form>
    </div>
    <?php

} // Handle export action function custom_post_type_export_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_export')) { wp_die('Access Denied'); }

}
// Handle export action
function custom_post_type_export_action() {
    if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_export')) {
        wp_die('Access Denied');
    }

    $filename = 'custom_post_type_export.csv';
    $data = array();

    // Query the custom post type
    $args = array(
        'post_type' => 'fr-teams',
        'posts_per_page' => -1,
    );
    $query = new WP_Query($args);

    // Loop through the custom post type posts
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            // Customize the data you want to export (example: post title and content)
            $post_data = array(
                'Title' => get_the_title(),
                'Content' => get_the_content(),
            );

            // Add post data to the export array
            $data[] = $post_data;
        }
    }

    wp_reset_postdata();

    // Generate CSV file
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    $output = fopen('php://output', 'w');
    $header_row = array_keys($data[0]);
    fputcsv($output, $header_row);
    foreach ($data as $row) {
        fputcsv($output, $row);
    }
    fclose($output);

    exit();

} add_action('admin_post_custom_post_type_export', 'custom_post_type_export_action');

// Handle import action function custom_post_type_import_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_import')) { wp_die('Access Denied'); }

}
add_action('admin_post_custom_post_type_export', 'custom_post_type_export_action');


// Handle import action
function custom_post_type_import_action() {
    if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_import')) {
        wp_die('Access Denied');
    }

    if (isset($_FILES['import_file']['tmp_name']) && $_FILES['import_file']['tmp_name']) {
        $file = $_FILES['import_file']['tmp_name'];
        $handle = fopen($file, 'r');

        if ($handle) {
            // Skip the header row
            fgetcsv($handle);

            while (($data = fgetcsv($handle)) !== false) {
                // Customize the import logic here (example: create custom post type posts)
                $post_data = array(
                    'post_type' => 'fr-teams',
                    'post_title' => $data[0], // Assuming the post title is in the first column
                    'post_content' => $data[1], // Assuming the post content is in the second column
                    // Add more post data fields if necessary
                );

                wp_insert_post($post_data);
            }

            fclose($handle);
        }
    }

    // Redirect to the custom post type listing page
    wp_redirect(admin_url('edit.php?post_type=fr-teams'));
    exit();
}
add_action('admin_post_custom_post_type_import', 'custom_post_type_import_action');

} add_action('admin_post_custom_post_type_import', 'custom_post_type_import_action');

// Add export/import options to admin menu function add_custom_post_type_export_import_menu() { add_submenu_page( 'edit.php?post_type=fr-teams', 'Export/Import', 'Export/Import', 'manage_options', 'custom_post_type_export_import', 'custom_post_type_export_import_page' ); } add_action('admin_menu', 'add_custom_post_type_export_import_menu');

// Render export/import page content function custom_post_type_export_import_page() { ?>

Custom Post Type Export/Import

    <h2>Export</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
        <input type="hidden" name="action" value="custom_post_type_export">
        <?php wp_nonce_field('custom_post_type_export'); ?>
        <p>
            <input type="submit" class="button button-primary" value="Export Custom Post Type">
        </p>
    </form>
    
    <h2>Import</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="custom_post_type_import">
        <?php wp_nonce_field('custom_post_type_import'); ?>
        <p>
            <input type="file" name="import_file">
        </p>
        <p>
            <input type="submit" class="button button-primary" value="Import Custom Post Type">
        </p>
    </form>
</div>
<?php

} // Handle export action function custom_post_type_export_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_export')) { wp_die('Access Denied'); }

$filename = 'custom_post_type_export.csv';
$data = array();

// Query the custom post type
$args = array(
    'post_type' => 'fr-teams',
    'posts_per_page' => -1,
);
$query = new WP_Query($args);

// Loop through the custom post type posts
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // Customize the data you want to export (example: post title and content)
        $post_data = array(
            'Title' => get_the_title(),
            'Content' => get_the_content(),
        );

        // Add post data to the export array
        $data[] = $post_data;
    }
}

wp_reset_postdata();

// Generate CSV file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$output = fopen('php://output', 'w');
$header_row = array_keys($data[0]);
fputcsv($output, $header_row);
foreach ($data as $row) {
    fputcsv($output, $row);
}
fclose($output);

exit();

} add_action('admin_post_custom_post_type_export', 'custom_post_type_export_action');

// Handle import action function custom_post_type_import_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_import')) { wp_die('Access Denied'); }

if (isset($_FILES['import_file']['tmp_name']) && $_FILES['import_file']['tmp_name']) {
    $file = $_FILES['import_file']['tmp_name'];
    $handle = fopen($file, 'r');

    if ($handle) {
        // Skip the header row
        fgetcsv($handle);

        while (($data = fgetcsv($handle)) !== false) {
            // Customize the import logic here (example: create custom post type posts)
            $post_data = array(
                'post_type' => 'fr-teams',
                'post_title' => $data[0], // Assuming the post title is in the first column
                'post_content' => $data[1], // Assuming the post content is in the second column
                // Add more post data fields if necessary
            );

            wp_insert_post($post_data);
        }

        fclose($handle);
    }
}

// Redirect to the custom post type listing page
wp_redirect(admin_url('edit.php?post_type=fr-teams'));
exit();

} add_action('admin_post_custom_post_type_import', 'custom_post_type_import_action');

// Add export/import options to admin menu
function add_custom_post_type_export_import_menu() {
    add_submenu_page(
        'edit.php?post_type=fr-teams',
        'Export/Import',
        'Export/Import',
        'manage_options',
        'custom_post_type_export_import',
        'custom_post_type_export_import_page'
    );
}
add_action('admin_menu', 'add_custom_post_type_export_import_menu');


// Render export/import page content
function custom_post_type_export_import_page() {
    ?>
    <div class="wrap">
        <h1>Custom Post Type Export/Import</h1>
        
        <h2>Export</h2>
        <form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
        <input type="hidden" name="action" value="custom_post_type_export">
        <?php wp_nonce_field('custom_post_type_export'); ?>
        <p>
            <input type="submit" class="button button-primary" value="Export Custom Post Type">
        </p>
    </form>
    
    <h2>Import</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="custom_post_type_import">
        <?php wp_nonce_field('custom_post_type_import'); ?>
        <p>
            <input type="file" name="import_file">
        </p>
        <p>
            <input type="submit" class="button button-primary" value="Import Custom Post Type">
        </p>
    </form>
    </div>
    <?php
}
// Handle export action
function custom_post_type_export_action() {
    if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_export')) {
        wp_die('Access Denied');
    }

    $filename = 'custom_post_type_export.csv';
    $data = array();

    // Query the custom post type
    $args = array(
        'post_type' => 'fr-teams',
        'posts_per_page' => -1,
    );
    $query = new WP_Query($args);

    // Loop through the custom post type posts
    if ($query->have_posts()) {
        while ($query->have_posts()) {
            $query->the_post();

            // Customize the data you want to export (example: post title and content)
            $post_data = array(
                'Title' => get_the_title(),
                'Content' => get_the_content(),
            );

            // Add post data to the export array
            $data[] = $post_data;
        }
    }

    wp_reset_postdata();

    // Generate CSV file
    header('Content-Type: text/csv');
    header('Content-Disposition: attachment; filename="' . $filename . '"');
    $output = fopen('php://output', 'w');
    $header_row = array_keys($data[0]);
    fputcsv($output, $header_row);
    foreach ($data as $row) {
        fputcsv($output, $row);
    }
    fclose($output);

    exit();
}
add_action('admin_post_custom_post_type_export', 'custom_post_type_export_action');


// Handle import action
function custom_post_type_import_action() {
    if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_import')) {
        wp_die('Access Denied');
    }

    if (isset($_FILES['import_file']['tmp_name']) && $_FILES['import_file']['tmp_name']) {
        $file = $_FILES['import_file']['tmp_name'];
        $handle = fopen($file, 'r');

        if ($handle) {
            // Skip the header row
            fgetcsv($handle);

            while (($data = fgetcsv($handle)) !== false) {
                // Customize the import logic here (example: create custom post type posts)
                $post_data = array(
                    'post_type' => 'fr-teams',
                    'post_title' => $data[0], // Assuming the post title is in the first column
                    'post_content' => $data[1], // Assuming the post content is in the second column
                    // Add more post data fields if necessary
                );

                wp_insert_post($post_data);
            }

            fclose($handle);
        }
    }

    // Redirect to the custom post type listing page
    wp_redirect(admin_url('edit.php?post_type=fr-teams'));
    exit();
}
add_action('admin_post_custom_post_type_import', 'custom_post_type_import_action');
Source Link

// Add export/import options to admin menu function add_custom_post_type_export_import_menu() { add_submenu_page( 'edit.php?post_type=fr-teams', 'Export/Import', 'Export/Import', 'manage_options', 'custom_post_type_export_import', 'custom_post_type_export_import_page' ); } add_action('admin_menu', 'add_custom_post_type_export_import_menu');

// Render export/import page content function custom_post_type_export_import_page() { ?>

Custom Post Type Export/Import

    <h2>Export</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>">
        <input type="hidden" name="action" value="custom_post_type_export">
        <?php wp_nonce_field('custom_post_type_export'); ?>
        <p>
            <input type="submit" class="button button-primary" value="Export Custom Post Type">
        </p>
    </form>
    
    <h2>Import</h2>
    <form method="post" action="<?php echo admin_url('admin-post.php'); ?>" enctype="multipart/form-data">
        <input type="hidden" name="action" value="custom_post_type_import">
        <?php wp_nonce_field('custom_post_type_import'); ?>
        <p>
            <input type="file" name="import_file">
        </p>
        <p>
            <input type="submit" class="button button-primary" value="Import Custom Post Type">
        </p>
    </form>
</div>
<?php

} // Handle export action function custom_post_type_export_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_export')) { wp_die('Access Denied'); }

$filename = 'custom_post_type_export.csv';
$data = array();

// Query the custom post type
$args = array(
    'post_type' => 'fr-teams',
    'posts_per_page' => -1,
);
$query = new WP_Query($args);

// Loop through the custom post type posts
if ($query->have_posts()) {
    while ($query->have_posts()) {
        $query->the_post();

        // Customize the data you want to export (example: post title and content)
        $post_data = array(
            'Title' => get_the_title(),
            'Content' => get_the_content(),
        );

        // Add post data to the export array
        $data[] = $post_data;
    }
}

wp_reset_postdata();

// Generate CSV file
header('Content-Type: text/csv');
header('Content-Disposition: attachment; filename="' . $filename . '"');
$output = fopen('php://output', 'w');
$header_row = array_keys($data[0]);
fputcsv($output, $header_row);
foreach ($data as $row) {
    fputcsv($output, $row);
}
fclose($output);

exit();

} add_action('admin_post_custom_post_type_export', 'custom_post_type_export_action');

// Handle import action function custom_post_type_import_action() { if (!current_user_can('manage_options') || !isset($_POST['_wpnonce']) || !wp_verify_nonce($_POST['_wpnonce'], 'custom_post_type_import')) { wp_die('Access Denied'); }

if (isset($_FILES['import_file']['tmp_name']) && $_FILES['import_file']['tmp_name']) {
    $file = $_FILES['import_file']['tmp_name'];
    $handle = fopen($file, 'r');

    if ($handle) {
        // Skip the header row
        fgetcsv($handle);

        while (($data = fgetcsv($handle)) !== false) {
            // Customize the import logic here (example: create custom post type posts)
            $post_data = array(
                'post_type' => 'fr-teams',
                'post_title' => $data[0], // Assuming the post title is in the first column
                'post_content' => $data[1], // Assuming the post content is in the second column
                // Add more post data fields if necessary
            );

            wp_insert_post($post_data);
        }

        fclose($handle);
    }
}

// Redirect to the custom post type listing page
wp_redirect(admin_url('edit.php?post_type=fr-teams'));
exit();

} add_action('admin_post_custom_post_type_import', 'custom_post_type_import_action');