WordPress

jak utworzyć opcje motywu eksportu / importu w wordpress bez wtyczki

  • 4 października, 2016
  • 3 min read
jak utworzyć opcje motywu eksportu / importu w wordpress bez wtyczki


// Dodaj opcje eksportu/importu do menu administratora function add_custom_post_type_export_import_menu() { add_submenu_page( 'edit.php?post_type=fr-teams’, 'Eksport/Import’, 'Eksport/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’);

// Renderuj funkcję eksportu/importu zawartości strony custom_post_type_export_import_page() { ?>

    <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

} // Obsługa akcji eksportu 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(’Odmowa dostępu’); }

$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’);

Warto przeczytać!  rozwój motywu - Uzyskanie efektów 3D Swiper Core działających w WordPress

// Obsługa akcji importu 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(’Odmowa dostępu’); }

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’);


Źródło