Automating WordPress Theme Options – Part 1

posted in: Blog, Tech Journal | 0

Introduction

The purpose of this article is to explore and create a way to automate the copying of theme settings from one WordPress site installation to another.

Context

A common workflow when adding a new feature or design component to a WordPress site is

  • Experiment with theme options in a sandbox site until the desired effect is achieved.
  • Figure out what theme options you just changed.
  • Replicate those exact theme options to the target site (e.g. live site).

For this article the intention is to accomplish the automation using a bash script as opposed to, say a PHP approach.

Approach

Automate these steps:

  1. Export theme options to a file.
  2. Diff the new file against a previous version of the theme options file to determine which specific options were changed and to what values.
  3. Use the diff values to create a script or command that can be executed on the target site on a target server to effect those same changes in the target environment.
  4. Execute the script or command on the target server.

Step 1 - export theme settings

WP options list

The wp-cli tool has a subcommand to export options. Let's explore to find the specific command that will export theme options.

  • The command wp options list dumps the entire list of options. This takes a while to run, it dumps way more options than just theme options and is overkill. Let's look at ways to filter the output.
  • It looks like theme options are stored in a single option with a name theme_mods_{theme name}. For example, for theme Virtue, the theme settings are stored in an option called theme_mods_virtue.
  • The command wp option list --search="theme_mods_*" outputs a list of settings for all installed themes as a table. Although I use my standard go-to theme for most projects, sometimes I use a child theme and sometimes I make the exception and use a different theme. Therefore, I will use this command (instead of filtering by theme) and let the subsequent diff determine for me which options I actually changed.
  • Let's explore other output formats however. I probably don't want to use the tabular display in my automated workflow.
  • Two possibilities:

    • wp option list --search="theme_mods_*" --format=json
    • wp option list --search="theme_mods_*" --format=yaml

Which format will I choose, json or yaml? It depends on which one works better with the subsequent diff and import commands.

Theme Virtue has an export theme settings function which exports them as a json file so that may be the one to stick with.

Observations

Because of the way WordPress serializes its theme settings, trying to isolate individual theme settings that have changed may be more problematic than they are worth. It appears that an adequate approach would be to update the entire set of theme settings in one go. According to user jay.lee,

Basically if you looking to only modify one element for performance reasons, I wouldn't worry about it. WordPress does so many things behind the scenes that will probably crush any optimization you manage to achieve. If you're just wanted to do it, sorry, I think it's a no go.

Conclusions

In light of these observations, I think the original approach needs to be modified and in the follow up article I'll skip over the diff step and proceed to how to import these theme settings on a target site.