How to Stop WordPress Theme Updates from Overwriting Custom Code
During a server migration to an AWS Lightsail Amazon Linux blueprint, I safely migrated and restored custom modifications across four separate areas into a child theme (lightning-child) so they won't get wiped out whenever the parent theme (Lightning) updates. Here is a log of the entire process.
1. Child Theme Files Created
Root directory of the new child theme:/var/www/html/wp-content/themes/lightning-child/
| 対象ファイルのパス | 作業区分 | カスタマイズの目的と概要 |
|---|---|---|
style.css | 新規追加 | 子テーマの定義 |
functions.php | 新規追加+コード追記 | 各種機能の追加(アーカイブ整理・日本語スラッグ変換) |
_g3/template-parts/next-prev.php | コピー+コード修正 | 前後の記事の同カテゴリ限定化 |
_g3/template-parts/sidebar-contents-post.php | コピー+コード修正 | サイドバー「最近の投稿」の同カテゴリ限定化 |
_g3/template-parts/page-header.php | コピー+コード修正 | カテゴリートップへの画像[show] |
2. Detailed Customizations by File
For the full code snippets and complete implementation details, please refer to the original post: "Summary of WordPress Custom Code Locations."
- style.css: Added the required header declarations so WordPress recognizes it as a child theme.
- next-prev.php: Modified previous/next post links to navigate strictly within the same category.
- sidebar-contents-post.php: Added query conditions to fetch and display posts only from the current category.
- page-header.php: Built custom logic to integrate with a plugin, overlaying the category title over the header image.
- functions.php: Configured archive layout adjustments and automated conversion for Japanese URL slugs.
3. Server Permissions & Security Settings (Crucial)
Once the changes were in place, file ownership and permissions were properly reconfigured to ensure WordPress can securely read and write files.
sudo chown -R www-data:www-data /var/www/html/wp-content/themes/lightning-child/
find /var/www/html/wp-content/themes/lightning-child/ -type f -exec chmod 644 {} ;
With this setup, the site is now safely insulated from future parent theme updates.
Troubleshooting the Japanese Slug Conversion
This feature wouldn't work at first and proved pretty tricky to solve, but Gemini guided me through the fix.
1. Switched the hooked action function
2. Flushed the permalinks by re-saving under Settings > Permalinks


