What is the best way to export and import theme options?

So what is the best way to create an export and import feature, I tried serialize() and urlencode() on an array of options to be used in a url like so:

export.php?data=(long string), and use $_GET method in export.php file which forces download using headers, but that’s not a good idea because the array is very large.

Read More

so what is the best way to do export and import theme options ?

Related posts

Leave a Reply

1 comment

  1. I’m just about done adding my own admin panel class an Import/ Export functionality and I’ve come to a point where i feel the best way to export data would be as a base64 encoded serialize array, and this is way,

    serialize data alone is somewhat human readable and i wonted to avoid that (theme options have stored files,images url , paths and other sensitive data), hence the base64 encoding.

    Now i agree this makes a mess of a data but a simple single line string comes out which is very easy to copy paste without making mistakes (like removing lines, brackets, parentheses …).

    As for the size of $_GET depends on used browser. Some support 2000 character,
    some more. I have a rule of thumb to use $_POST method, if send data has more than
    255 character.

    Example , say $options is an array of theme options then export is done like this

    echo "<!*!* START export Code !*!*>n".base64_encode(serialize($options))."n<!*!* END export Code !*!*>";
    

    and the import code is:

    $import_code = $_POST['import_code'];
    $import_code = str_replace("<!*!* START export Code !*!*>n","",$import_code);
    $import_code = str_replace("n<!*!* END export Code !*!*>","",$import_code);
    $import_code = base64_decode($import_code);
    $import_code = unserialize($import_code);
    

    this is actually the code i use in my plugin Shortcodes UI for the import export functionality.

    Update

    As for downloading the export dump, you don’t have to create a more the one request meaning that you can export to a file with a single request, ex:

    //first  add a new query var
    public function add_query_var_vars() {
        global $wp;
        $wp->add_query_var('theme_export_options');
    }
    
    //then add a template redirect which looks for that query var and if found calls the download function
    public function admin_redirect_download_files(){
        global $wp;
        global $wp_query;
        //download theme export
        if (array_key_exists('theme_export_options', $wp->query_vars) && $wp->query_vars['theme_export_options'] == 'safe_download'){
            download_file();
            die();
        }
    }
    
    
    //add hooks for these functions
    add_action('template_redirect', 'admin_redirect_download_files');
    add_filter('init', 'add_query_var_vars');
    
    
    //then define the function that will take care of the actual download
    public function download_file($content = null, $file_name = null){
        if (! wp_verify_nonce($_REQUEST['nonce'], 'theme_export_options') ) 
            wp_die('Security check'); 
    
        //here you get the options to export and set it as content, ex:
        $options= get_option('my_theme_options');
        $content = "<!*!* START export Code !*!*>n".base64_encode(serialize($options))."n<!*!* END export Code !*!*>";
        $file_name = 'theme_export.txt';
        header('HTTP/1.1 200 OK');
    
    
        if ( !current_user_can('edit_themes') )
            wp_die('<p>'.__('You do not have sufficient permissions to edit templates for this site.').'</p>');
        }
        if ($content === null || $file_name === null){
            wp_die('<p>'.__('Error Downloading file.').'</p>');     
        }
        $fsize = strlen($content);
        header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
        header('Content-Description: File Transfer');
        header("Content-Disposition: attachment; filename=" . $file_name);
        header("Content-Length: ".$fsize);
        header("Expires: 0");
        header("Pragma: public");
        echo $content;
        exit;
    }
    
    
    //and last a simple function to create the download export url / link
    function create_export_download_link($echo = false){
        $site_url = get_bloginfo('url');
        $args = array(
            'theme_export_options' => 'safe_download',
            'nonce' => wp_create_nonce('theme_export_options')
        );
        $export_url = add_query_arg($args, $site_url);
        if ($echo === true)
            echo '<a href="'.$export_url.'" target="_blank">Download Export</a>';
        elseif ($echo == 'url')
            return $export_url;
        return '<a href="'.$export_url.'" target="_blank">Download Export</a>';
    }
    

    So all i have to do is add a call to create_export_download_link(true); in my theme options panel and it will create a link like this :

    <a href="http://domain.com/?theme_export_options=safe_download&nonce=as4d56as4" target="_blank">Download Export</a>
    

    obviously you will need to change it up a bit to match your needs but you should get the idea.