In our previous articles, we have explained how to backup WordPress. But how can we backup WordPress theme? Is it even possible? When should you backup a WordPress theme?
Getting a backup from a WordPress theme is essential if you are going to make major changes to your active theme. Especially if you have deeply customized the theme to match your business and brand. If anything goes wrong with the theme, you can’t just re-download it. You will have to re-do everything from step 0.
We also have an article that discusses some of the best WordPress backup plugins that you can check out.
In this article, we are going to teach you how to backup WordPress theme with FTP, plugin and code.
How to Backup a WordPress Theme?
To backup a WordPress theme with FTP, you will need to have an FTP account and an FTP client. The FTP account should be provided when you purchased the host. There are many popular FTP clients like FireFTP, gFTP, FileZilla and etc. In this article, we will teach you how to backup a WordPress theme using the FileZilla client. Follow these steps:
- Download FileZilla from their official website.
- Head over to the WordPress dashboard and click on Appearance.
- Check which theme is activated.
- Login to FileZilla with credential given to you by the hosting provider. If you have lost it or the hosting provider didn’t give you the FTP credential, feel free to contact them and ask for your FTP details.
- From FileZilla go to your server and find the theme’s folder. By default, every theme is installed in wp-content > themes.
- Download the activated theme on your computer and make sure FTP says the file transferred correctly.
And that’s how you backup a WordPress theme with FileZilla.
Backup a WordPress Theme with UpdraftPlus
Most WordPress users find FTP a bit difficult and confusing and they rather stick to using plugins for their needs.
One of the most popular plugins to backup WordPress is UpdraftPlus. This plugin has over 2 million active installs and it is compatible with the latest version of WordPress.
With UpdraftPlus plugin, you can directly upload backups to Dropbox, Google Drive, Amazon S3 and etc. To download and use UpdraftPlus, follow the steps below:
- Download, install and activate UpdraftPlus.
- Go to the WordPress dashboard, hover over Plugins and click on Add New. Then, in the search box type in UpdraftPlus.
- Click on Install now then Activate.
- Once activated, from Settings click on UpdraftPlus Backups.
- The first tab is Backup / Restore.
- In the first tab, click on “Backup Now”.
- A message pops up. It asks you whether you want to include your database and files in the backup or not. Click on the (…) to see more information.
- In this example, we are going to choose Themes alone and click on Backup Now.
- Once the backup is finished, you should get a message saying backup is finished and in the same tab, you should be able to see the following.
- By clicking on Themes, you will get the options to download it to your computer, delete from your web server or browse its contents. In addition, you are given the options to delete the backup or restore your website.
- In the Migrate / Clone tab, you are given an option to create a temporary clone of your WordPress website on UpdraftPlus’ servers.
UpdraftPlus is available in both premium and free versions. But you can backup WordPress theme through the free version.
Backup WordPress Theme with the Editor
Other than the methods mentioned above, you can also use the WordPress editor to backup the activated theme. To do so, follow the steps below:
- Go to the WordPress dashboard and click on Appearance.
- Click on Theme Editor to open the Edit Themes page.
- You will be able to see all the files related to the activated theme or the theme you would like to backup.
- Choose the first file in the list. Copy all the code and paste it in an empty PHP file or a text editor like Notepad++.
- Save the file with the same file name as the template you have opened.
- Do this for every file related to the theme.
This method is very time-consuming compared to the other ways mentioned above. However, it is just as much effective.
How to Create Backup and Restore Theme Options in WordPress via Code?
If you are an advanced user with a little bit of programming knowledge and don’t want to install any extra plugins, then we recommend following this method.
To implement this, you will have to copy and paste the code below in your theme’s functions.php file to add the backup and restore feature to it:
/*
Backup/Restore Theme Options
Go to "Appearance > Backup Options" to export/import theme settings
*/
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();