Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

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);
}
}
}
?>
share|improve this question
    
If you've got some working code that solves your problem, you should post it as an answer. That way, if you get it working the way you want, you can accept that answer as the one that best solves the problem. :) –  Chip Bennett May 7 '12 at 17:13
1  
take a look at this answer wordpress.stackexchange.com/a/50746/2487 –  Bainternet May 7 '12 at 20:28
add comment

2 Answers

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 :)

share|improve this answer
1  
I would strongly recommend an approach that doesn't use fopen() and related functions. Use the WordPress Filesystem API instead, if you must write files. –  Chip Bennett May 7 '12 at 16:34
    
yep Chip is right, i was thinking in generic PHP, but in Wordpress it would be better to use WordPress Filesystem API. –  Tribalpixel May 7 '12 at 16:39
    
array are serialized in database. look at serialziation in function reference of WP codex: codex.wordpress.org/Function_Reference/maybe_unserialize codex.wordpress.org/Function_Reference/maybe_serialize –  Tribalpixel May 7 '12 at 21:15
add comment

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);
}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.