WordPress Backup and Restore Theme Options

After taking the time to set a whole bunch of these options, it’s nice to be able to make a quick WordPress Backup of your theme settings. Many themes have this functionality built-in, but for themes that don’t, here is a plug-n-play snippet to create a ” WordPress Backup/Restore Theme Options” page. You can see the technique in action in my shapeSpace theme (100% free and open-source WordPress starter theme).

WordPress Backup

Overview

This is a basic technique that uses a PHP class to create a “Backup/Restore Options” page in the WP Admin. After adding the code, the Appearance menu will contain a “Backup Options” link leading to the WordPress Backup/Restore Options page. There, you may WordPress Backup/export your theme’s options at your convenience, as shown in this screenshot:

Backup Theme Options from the WP Admin

If/when you need to restore your previous settings, use the “Restore/Import” option to select, upload, and apply the backup settings, as shown here:

Restore Theme Options from the WP Admin

If that’s something that would be useful for you, read on to learn how to implement and customize in your own WordPress theme.

Backup and Restore Theme Options

To implement this WordPress Backup/restore technique, open your theme’s functions.php file and include the following code:

/*
	Backup/Restore Theme Options
	@ https://digwp.com/2014/04/backup-restore-theme-options/
	Go to "Appearance > Backup Options" to export/import theme settings
	(based on "Gantry Export and Import Options" by Hassan Derakhshandeh)

	Usage:
	1. Add entire backup/restore snippet to functions.php
	2. Edit 'shapeSpace_options' to match your theme options
*/
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() {
		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="theme-options-'.date("dMy").'.dat"');
			echo serialize($this->_get_options());
			die();
		}
		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));
					}
				}
			}
			wp_redirect(admin_url('themes.php?page=backup-options'));
			exit;
		}
	}
	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">
					<tr>
						<td>
							<h3>Backup/Export</h3>
							<p>Here are the stored settings for the current theme:</p>
							<p><textarea class="widefat code" rows="20" cols="100" onclick="this.select()"><?php echo serialize($this->_get_options()); ?></textarea></p>
							<p><a href="?page=backup-options&action=download" class="button-secondary">Download as file</a></p>
						</td>
						<td>
							<h3>Restore/Import</h3>
							<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'); ?>
						</td>
					</tr>
				</table>
			</form>
		</div>

	<?php }
	function _display_options() {
		$options = unserialize($this->_get_options());
	}
	function _get_options() {
		global $wpdb;
		return $wpdb->get_results("SELECT option_name, option_value FROM {$wpdb->options} WHERE option_name = 'shapeSpace_options'"); // edit 'shapeSpace_options' to match theme options
	}
}
new backup_restore_theme_options();

As credited in the code, this technique is based on Gantry Export and Import Options, and there are probably other plugins that perform similar functionality.

Usage

After including this in your theme’s functions template, edit the only instance of withshapeSpace_options the name of your theme options. If your theme doesn’t have its own options, you can still see it work by replacing withshapeSpace_options an option that already exists in the WP database, like orwidget_recent-commentsactive_plugins.

No other configuration of the code should be necessary unless you would like to customize, extend, etc. It’s designed for plug-n-play functionality, so it should work out of the box. I’ve only tested this on a handful of themes, however, so your mileage may vary.

Update!

The previous backup/restore script works great for parent themes but needs a bit of tweaking to work with Child Themes. Thankfully, Paul Taylor over at Old Castle Web shares his child-theme-friendly version that’s ready to go. You can download it here. Cheers! 🙂