0

I am creating a theme with settings-api and I've have done a lot and it's almost ended. Now I need to export and import functionality where I am stuck from last few days. I have tried a lot of codes and didn't found a good way.

Sample code which I am using to create theme options is:-

function display_twitter_element()
{
    ?>
        <input type="text" name="twitter_url" id="twitter_url" value="<?php echo get_option('twitter_url'); ?>" />
    <?php
}
function display_facebook_element()
{
    ?>
        <input type="text" name="facebook_url" id="facebook_url" value="<?php echo get_option('facebook_url'); ?>" />
    <?php
}
function display_theme_panel_fields()
{
    add_settings_section("section", "All Settings", null, "theme-options");
    add_settings_field("twitter_url", "Twitter Profile Url", "display_twitter_element", "theme-options", "section");
    add_settings_field("facebook_url", "Facebook Profile Url", "display_facebook_element", "theme-options", "section");
    register_setting("section", "twitter_url");
    register_setting("section", "facebook_url");
}
add_action("admin_init", "display_theme_panel_fields");
6
  • can u give any clue how to change it and remove from uniqueness @andy Macaulay-Brook
    – neil
    Commented Oct 4, 2016 at 8:26
  • and how to do it after removing my option from uniqueness @andy Macaulay-Brook
    – neil
    Commented Oct 4, 2016 at 8:27
  • I deleted the comment before posting as I realised it wasn't relevant! My phone app seems to have posted half of it anyway, sorry. Commented Oct 4, 2016 at 8:28
  • Could you be more specific about what you are trying to import/export and what you have done so far? The code you are showing is a partial implementation of the settings api that won't do anything in itself.
    – cjbj
    Commented Oct 4, 2016 at 9:44
  • HI i have created a complete wordpress theme with help of this link www.sitepoint.com/create-a-wordpress-theme-settings-page-with-the-settings-api -- Now i want to add export and import theme options menu. and i need to make it without plugin
    – neil
    Commented Oct 4, 2016 at 10:21

1 Answer 1

-1
// 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');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.