Keeping Custom WordPress Behavior Through a Theme Update

The two sites had redirects and page rules inside their parent themes. Updating those themes would replace the customized files. The migration moved the existing behavior into small must-use plugins, with a guard to prevent the plugin and old theme from registering it twice.

This case study documents an AI-assisted migration on two WordPress sites I maintain. AI tools helped inspect the existing code, develop the migration and challenge its failure cases. Isolated tests and operator-run updates supplied the evidence described here.

The starting point was a comparison with the original theme packages. Both installations contained custom behavior in their parent themes. The migration preserved those additions while replacing the themes with their official updated packages. Existing redirects, title handling and structured-data behavior formed the baseline. Changing their purpose at the same time would have made it harder to tell whether a difference came from the migration or a new design decision.

A small must-use plugin on each site provided a home for that behavior. WordPress automatically loads must-use plugins before ordinary plugins. They also carry a maintenance responsibility: they cannot be switched off through the ordinary plugin controls, and they do not receive normal plugin update notifications. That made keeping these files small and documented part of the solution.

Publishing an unconditional copy of the hooks would allow both locations to register the same behavior. That can matter even when a callback appears harmless. Redirects, output buffers and named functions each have different failure modes when duplicated. Removing the old code first would create a different intermediate state, with the behavior temporarily absent.

The migration therefore distinguished three states:

  • Original customized theme: the existing theme owns the behavior.
  • Original theme plus the new plugin: the plugin recognizes the verified legacy build and stays inert.
  • Updated theme plus the new plugin: the plugin registers the preserved hooks.

Each MU file initially registers an anonymous callback on after_setup_theme, which WordPress fires after loading the theme. The preserved hooks are registered inside that callback, after the legacy-build check. On the site with named helper functions, those declarations also sit below the early return: PHP reaches them only when the plugin should take responsibility. The other site uses anonymous callbacks. Deferring hook registration alone would not prevent a collision if the MU file declared the old theme’s global function names before that check.

Each site’s guard matches one exact legacy version through its theme’s loaded version constant, HELLO_ELEMENTOR_VERSION or ASTRA_THEME_VERSION. Before deployment, the installed theme was compared with the official legacy package plus the exact reviewed customization, and the runtime version declaration was confirmed. That connected the guard’s value to the customized build it expected. The guard does not cover a range of older releases.

The timing matters because a request and the filesystem can briefly describe different states. A request may have loaded the old customized theme before an update replaces its files. Looking only at the replacement files could suggest that the new plugin should take over, even though the running request already has the old hooks. Using the loaded declaration kept the decision aligned with that request.

This was a narrow compatibility rule for verified original builds. It depended on confirming which builds contained the customization. A version string by itself cannot establish that a theme contains particular code, so this approach should not be copied into an unrelated installation without checking that assumption.

Recovery required the captured customized theme, with the compatibility plugin left installed. Once that original build loaded again, the plugin’s early return would leave its hooks in charge. Installing a clean official package of the same legacy version would break that assumption: the version would match, but the custom behavior would be missing.

Before replacement, the process captured current recovery copies and checked archive integrity. It published the plugin without overwriting an existing file, updated the theme, then recursively compared the resulting theme directory with the unpacked target package without following symlinks. Separately, each plugin returns immediately when WP_CLI is defined and true, before registering any of its preserved hooks. That boundary keeps these callbacks out of WP-CLI maintenance commands.

Verification covered several different questions. Thirty-seven automated, isolated behavior checks exercised the hook implementation. Six automated migration scenarios, within a larger maintenance suite, covered success and failures around source validation, recovery capture, plugin publication, updating and final package comparison. In the simulated updater failure, the checks required the recovery copy and compatibility plugin to remain available while temporary working files were cleaned up.

The actual updates then completed on both sites. Served-page checks confirmed that page titles, canonical URLs and Person structured data matched the preserved baseline. On one site, all five selected redirects and the checked date-link behavior were preserved. Plugin and theme activation states also remained consistent.

Those results have limits. Archive integrity checks did not exercise a complete restore. Independent desktop/mobile layout, editor and form checks remained on the completion list at the time of writing. A successful redirect check cannot establish that a form delivers its message.

The custom behavior now lives in two small, versioned MU-plugin files. Both updated parent-theme directories passed their package comparisons, and the captured customized themes remain available for recovery.