Backing Up Theme Options

I have been exercised for quite a while about how to back up WordPress theme options. I wanted a system to export the existing options, and import old options. This seems to be particularly important, when moving a WordPress site from development server to deployment server, and vice versa.

Anyway, I found an excellent system by Jeff Starr at Digging Into Wordpress. There was just a minor problem. I use a lot of child themes, and the system would only work with a main theme, not with a child theme. This was, however, soon overcome, and my edited version of the code can be found below. Just copy and paste this code into the functions.php file of your main theme. In my version, there is no need to find and replace anything, because it will read the name of your theme, or associated child theme, direct from the options table in the database.

Alternatively, you could simply install the plugin that that I have made out of this code:

OCWS Options Export and Import

/* Section to save options */
Backup/Restore Theme Options
Go to “Appearance > Backup Options” to export/import theme settings
(based on “Gantry Export and Import Options” by Hassan Derakhshandeh)I (OCWS) have edited the code slightly, so that it works with Child Themes

1. Add entire backup/restore snippet to functions.php

class backup_restore_theme_options {

function backup_restore_theme_options() {
add_action('admin_menu', array(&$this, 'admin_menu'));
function admin_menu() {
// add_submenu_page($parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function);
// $page = add_submenu_page('themes.php', 'Backup Options', 'Backup Options', 'manage_options', 'backup-options', array(&$this, 'options_page'));

// add_theme_page($page_title, $menu_title, $capability, $menu_slug, $function);
$page = add_theme_page('Backup Options', 'Backup Options', 'manage_options', 'backup-options', array(&$this, 'options_page'));

add_action(“load-{$page}”, array(&$this, 'import_export'));
function import_export() {
$ocwsqt_option_name = get_option( 'stylesheet' );
if (isset($_GET['action']) && ($_GET['action'] == 'download')) {
header(“Cache-Control: public, must-revalidate”);
header(“Pragma: hack”);
header(“Content-Type: text/plain”);
header('Content-Disposition: attachment; filename=”'.$ocwsqt_option_name.'-options-'.date(“dMy”).'.dat”');
echo serialize($this->_get_options());
if (isset($_POST['upload']) && check_admin_referer('shapeSpace_restoreOptions', 'shapeSpace_restoreOptions')) {
if ($_FILES[“file”][“error”] > 0) {
// error
} else {
$options = unserialize(file_get_contents($_FILES[“file”][“tmp_name”]));
if ($options) {
foreach ($options as $option) {
update_option($option->option_name, unserialize($option->option_value));
function options_page() { ?>

<div class=”wrap”>
<?php screen_icon(); ?>
<h2>Backup/Restore Theme Options</h2>
<form action=”” method=”POST” enctype=”multipart/form-data”>
<style>#backup-options td { display: block; margin-bottom: 20px; }</style>
<table id=”backup-options”>
<p>Here are the stored settings for the current theme:</p>
<p><textarea class=”widefat code” rows=”20″ cols=”100″ onclick=””><?php echo serialize($this->_get_options()); ?></textarea></p>
<p><a href=”?page=backup-options&action=download” class=”button-secondary”>Download as file</a></p>
<p><label class=”description” for=”upload”>Restore a previous backup</label></p>
<p><input type=”file” name=”file” /> <input type=”submit” name=”upload” id=”upload” class=”button-primary” value=”Upload file” /></p>
<?php if (function_exists('wp_nonce_field')) wp_nonce_field('shapeSpace_restoreOptions', 'shapeSpace_restoreOptions'); ?>

<?php }
function _display_options() {
$options = unserialize($this->_get_options());
function _get_options() {
global $wpdb;

$ocwsqt_option_name = get_option( 'stylesheet' );

return $wpdb->get_results(“SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name = '”.$ocwsqt_option_name.”'”);
new backup_restore_theme_options();
/* End of options saving section */

2 responses on “Backing Up Theme Options

  1. Rip Noel

    Your code above is different from your code on the Website referenced by Jeff Starr. Can you please let everyone know which is the latest version? Thanks! Rip Noel

  2. pftaylor61 Post author

    The code on this page is the latest code. The code on is still Mr Starr's original code – but he has placed a link to the code on this page.

Leave a Reply

Your email address will not be published. Required fields are marked *