One of my tasks today was to activate a new child theme on a project. We’d already migrated the site to Pantheon (using their very straightforward migration plugin) and had activated the parent theme – in this case Newspack – to start playing around.
The Problem
My colleague Bart had completed initial work on the child theme so it was time to deploy that and activate the child theme, but that created a problem. We’d already done lots of configuration with the Newspack theme that we wanted to keep, but how?
Sidenote: Newspack has 6 themes and a nifty feature that means switching between them retains any configuration. Nice!
Theme Mods Option
Each theme has an option containing configuration. For the Newspack Theme this is theme_mods_newspack-theme
. For our new child theme this is theme_mods_clientname
.
All we really have to do then is copy the contents of the theme_mods_newspack-theme
option to our new theme_mods_clientname
option. But how?
Piping
We’re going to use the command line and WP-CLI for this. First, if you’re not aware of piping it’s a tool you can use on the command line to send the result of one command to the input of another.
For example, let’s say I want to check if my colleague Bart has a user account on the WordPress site I could just run wp user list
and look down the list, but what if that list is hundreds of users long?
The grep
command is a way to search a piece of content (or file) for a particular string. Maybe I have a text file called users.txt
and I want to search it for “Bart”. I can do so with grep Bart users.txt
.
Now we can combine the two by piping the output of WP CLI to the input of grep
like so:
wp user list | grep Greg
Now we’ll have only any users that contain “Greg”.
Updating the option
We can use the same principle to copy option values. Here’s how we’d get all the settings we’ve already configured with Newspack:
wp option get theme_mods_newspack-theme
To update our new child theme option we’d use
wp option update theme_mods_clientname [value]
So using piping we can simply copy the options with
wp option get theme_mods_newspack-theme | wp option update theme_mods_clientname
Through Terminus
That’s all well and good if you are on the server in a bash shell at the root of your WordPress install but we’re hosting with Pantheon, so we don’t have that option. What we do have is Terminus and it’s remote:wp
command.
Initially I was skeptical that I could simply pipe two Terminus commands to each other but it turns out it works just fine! All we need to do is modify our above copying command to run both WP CLI commands through terminus, like so:
terminus remote:wp clientname.dev -- option get theme_mods_newspack-theme | terminus remote:wp clientname.dev -- option update theme_mods_clientname
This will effectively run both WP CLI commands, piping the output of the first into the input of the second, copying the theme options from one theme to the other.
Easy peasy!
Leave a Reply