WordPress Theme Options Import/Export

I already have a theme options page that saves to wp_options > kittens_options.

I’m looking for a simple way to incorporate an import/export textarea of those options on my options page.

Read More

I’ve searched Google for several hours now and haven’t been able to find any useful tutorials. If anyone knows off the top of their head let me know.

Thanks

CODE I’M WORKING WITH SO FAR

<?php
function kittens_transport_page() {
global $themename, $shortname;
$kittens = get_option( 'kittens_options' );
$currentsettings = "";
if ( isset( $_POST['import'] ) && trim($_POST['kittens_import_settings']) != "" ) {
$currentsettings = $_POST['kittens_import_settings'];
} elseif ( isset( $kittens ) && ( $kittens != "" ) ) {
$currentsettings = base64_encode( serialize( $kittens ) );
}
}
?>
<div id="import-export">
<h2>Import &amp; Export Theme Options</h2>
<form method="post" action="#">
<label class="description">Import Options</label>
<textarea rows="8" cols="40" id="kittens_import_settings" name="kittens_import_settings" class="large-text"></textarea><br />
<input type="submit" value="Import" id="import" name="import" class="button-primary" onClick="return confirm('Are you sure you want to import these settings?')" /> <?php if ( isset( $_POST['import'] ) && $_POST['kittens_import_settings'] != "" ) { echo "Settings Imported Successfully"; } ?>
</form>
<br />
<br />
<label class="description">Export Options</label>
<textarea rows="8" cols="40" id="kittens_export_settings" name="kittens_export_settings" class="large-text" readonly="readonly"><?php echo $currentsettings; ?></textarea>
<?php
function kittens_import_settings(){
global $shortname;
if ( isset( $_POST['import'] ) && trim($_POST['kittens_import_settings']) != "" ) {
if(isset($_POST['kittens_import_settings']) && current_user_can('edit_themes')){
$imported_settings = unserialize(base64_decode($_POST['kittens_import_settings']));
update_option($shortname . '_settings', $imported_settings);
}
}
}
?>

Related posts

Leave a Reply

2 comments

  1. maybe you can write 2 functions for your options

    1. one script to write a txt file to export your options
    2. one script to import in your wordpress (add upload field in your option
      theme page)

    all of theses functions can be in your functions.php of the theme

    here is an example of function to write a ini (txt) file

    function write_ini_file($assoc_arr, $path, $has_sections=FALSE) { 
        $content = ""; 
        if ($has_sections) { 
            foreach ($assoc_arr as $key=>$elem) { 
                $content .= "[".$key."]n"; 
                foreach ($elem as $key2=>$elem2) { 
                    if(is_array($elem2)) 
                    { 
                        for($i=0;$i<count($elem2);$i++) 
                        { 
                            $content .= $key2."[] = "".$elem2[$i].""n"; 
                        } 
                    } 
                    else if($elem2=="") $content .= $key2." = n"; 
                    else $content .= $key2." = "".$elem2.""n"; 
                } 
            } 
        } 
        else { 
            foreach ($assoc_arr as $key=>$elem) { 
                if(is_array($elem)) 
                { 
                    for($i=0;$i<count($elem);$i++) 
                    { 
                        $content .= $key2."[] = "".$elem[$i].""n"; 
                    } 
                } 
                else if($elem=="") $content .= $key2." = n"; 
                else $content .= $key2." = "".$elem.""n"; 
            } 
        } 
    
        if (!$handle = fopen($path, 'w')) { 
            return false; 
        } 
        if (!fwrite($handle, $content)) { 
            return false; 
        } 
        fclose($handle); 
        return true; 
    }
    

    You can use write_ini_file to send the values to a file, parse_ini_file to read them back in 🙂

  2. Your code is messy, but seems ok.

    The only thing you need to do is to decode the $_POST‘ed data on import, so instead of:

    update_option( $shortname . '_settings', $_POST['kittens_import_settings'] );

    use:

    if(isset($_POST['kittens_import_settings']) && current_user_can('edit_themes')){
      $imported_settings = unserialize(base64_decode($_POST['kittens_import_settings']));
      update_option($shortname . '_settings', $imported_settings);
    }