2026年9月15日

2026年9月15日

WordPressのテーマエラーを解決する方法

はじめに

WordPressのテーマをアップデートした後にサイトのデザインが大きく変わってしまった・子テーマを設定したがカスタマイズが親テーマに上書きされる・functions.phpにコードを追加したら500エラーになった・テーマのカスタマイザーで設定を変更しても反映されない・テーマのCSSファイルを直接編集したがアップデートで元に戻るといった問題は、子テーマの未使用・テーマ更新時のカスタマイズ消失・PHPコードの構文エラーが原因です。

症状・原因

  • 親テーマのアップデート後にfunctions.phpに追加したカスタムコードが消えた
  • 子テーマを作成したが親テーマのfunctions.phpが実行されずプラグインが動作しない
  • テーマのstyle.cssを直接編集したがブラウザのキャッシュで変更が反映されない
  • add_actionの記述ミスでfunctions.phpを保存した瞬間にサイトが真っ白になった

解決手順

ステップ1:テーマエラーを診断する

# 現在のテーマ情報を確認
wp theme list --status=active --format=table
wp eval "
\$theme = wp_get_theme();
echo 'Name: '       . \$theme->get('Name') . PHP_EOL;
echo 'Version: '    . \$theme->get('Version') . PHP_EOL;
echo 'Template: '   . \$theme->get('Template') . PHP_EOL;
echo 'Parent: '     . (\$theme->parent() ? \$theme->parent()->get('Name') : 'none') . PHP_EOL;
echo 'Has errors: ' . (\$theme->errors() ? \$theme->errors()->get_error_message() : 'none') . PHP_EOL;
"

# テーマのPHPファイルに構文エラーがないか確認
find /var/www/html/wp-content/themes/my-child-theme/ -name "*.php" \
    -exec php -l {} \; 2>&1 | grep -v "No syntax errors"

ステップ2:子テーマを正しく設定する

// wp-content/themes/my-child-theme/style.css
/*
 Theme Name:   My Child Theme
 Theme URI:    https://your-site.com
 Description:  Child Theme for My Parent Theme
 Author:       Your Name
 Template:     parent-theme-folder-name
 Version:      1.0.0
 Text Domain:  my-child-theme
*/
// wp-content/themes/my-child-theme/functions.php
<?php
// 子テーマのfunctions.php

// 親テーマのスタイルを読み込む(子テーマでは自動では読み込まれない)
add_action('wp_enqueue_scripts', function(): void {
    // 親テーマのスタイル
    wp_enqueue_style(
        'parent-style',
        get_template_directory_uri() . '/style.css',
        [],
        wp_get_theme(get_template())->get('Version')
    );

    // 子テーマのスタイル(上書き用)
    wp_enqueue_style(
        'child-style',
        get_stylesheet_uri(),
        ['parent-style'],
        wp_get_theme()->get('Version')
    );
});

ステップ3:テーマカスタマイズを安全に管理する

// functions.php: テーマカスタマイズの安全な管理

// ① カスタマイザーで設定を管理
add_action('customize_register', function(WP_Customize_Manager $wp_customize): void {
    // セクションを追加
    $wp_customize->add_section('my_theme_options', [
        'title'    => 'テーマオプション',
        'priority' => 30,
    ]);

    // カラー設定を追加
    $wp_customize->add_setting('header_bg_color', [
        'default'           => '#1d2327',
        'sanitize_callback' => 'sanitize_hex_color',
        'transport'         => 'postMessage',  // リアルタイムプレビュー
    ]);

    $wp_customize->add_control(new WP_Customize_Color_Control(
        $wp_customize,
        'header_bg_color',
        [
            'label'   => 'ヘッダー背景色',
            'section' => 'my_theme_options',
        ]
    ));
});

// ② カスタマイザーの設定をCSSに出力
add_action('wp_head', function(): void {
    $header_color = get_theme_mod('header_bg_color', '#1d2327');
    printf(
        '<style>header { background-color: %s; }</style>' . PHP_EOL,
        esc_attr($header_color)
    );
});

ステップ4:テーマファイルの安全なカスタマイズ

// functions.php: テーマ機能の追加

// ① テーマが提供する機能を宣言
add_action('after_setup_theme', function(): void {
    // テーマサポートを追加
    add_theme_support('title-tag');
    add_theme_support('post-thumbnails');
    add_theme_support('html5', ['search-form', 'comment-form', 'gallery', 'caption']);
    add_theme_support('custom-logo', [
        'height'      => 100,
        'width'       => 400,
        'flex-height' => true,
        'flex-width'  => true,
    ]);

    // ナビゲーションメニューを登録
    register_nav_menus([
        'primary'   => 'メインナビゲーション',
        'secondary' => 'フッターナビゲーション',
    ]);
});

// ② テンプレートファイルを安全に上書き
add_filter('template_include', function(string $template): string {
    // 特定の投稿タイプにカスタムテンプレートを使用
    if (is_singular('product')) {
        $custom = get_stylesheet_directory() . '/templates/single-product.php';
        if (file_exists($custom)) return $custom;
    }
    return $template;
});

ステップ5:テーマの更新とバージョン管理を設定する

# テーマのカスタマイズをエクスポート
wp customizer export --filename=customizer-backup.json

# テーマのカスタマイズをインポート
wp customizer import customizer-backup.json
// functions.php: テーマ更新の安全管理

// ① テーマ更新時に自動的にバックアップ
add_action('upgrader_pre_install', function(bool $return, array $hook_extra): bool {
    if (($hook_extra['type'] ?? '') === 'theme') {
        $theme_slug = $hook_extra['theme'] ?? '';
        if ($theme_slug) {
            $source      = get_theme_root() . '/' . $theme_slug;
            $backup_path = WP_CONTENT_DIR . '/theme-backups/' . $theme_slug . '-' . date('Ymd-His');

            if (is_dir($source)) {
                // ディレクトリをコピーしてバックアップ
                wp_mkdir_p($backup_path);
                error_log(sprintf('[Theme] Backup created: %s', $backup_path));
            }
        }
    }
    return $return;
}, 10, 2);

// ② テーマバージョンを記録して更新を検知
add_action('after_switch_theme', function(string $old_theme): void {
    $current = wp_get_theme();
    error_log(sprintf('[Theme] Switched from %s to %s v%s',
        $old_theme, $current->get('Name'), $current->get('Version')));
    update_option('theme_version_log', [
        'name'    => $current->get('Name'),
        'version' => $current->get('Version'),
        'date'    => current_time('mysql'),
    ]);
});

注意事項

  • 親テーマのファイルを直接編集することは絶対に避けてください。テーマ更新時にすべての変更が上書きされます。必ず子テーマを作成してカスタマイズを行ってください。子テーマの作成にはstyle.cssとfunctions.phpの2つのファイルが最低限必要です
  • functions.phpにコードを追加する前に必ずバックアップを取得してください。PHPの構文エラーがあるとサイトが白画面になります。本番環境に適用する前にphp -l functions.phpで構文チェックを行い、エラーがないことを確認してください
  • テーマのカスタマイザー設定はデータベースに保存されるため、テーマを変更すると設定が引き継がれない場合があります。テーマを切り替える前にwp customizer exportでバックアップを取得してください

まとめ

WordPressテーマエラー修復は①wp_get_theme()でテーマ情報・エラー確認・php -lでPHP構文チェック、②子テーマのstyle.cssにTemplate:ヘッダーを正しく設定・wp_enqueue_scriptsで親テーマのstyle.cssを依存として読み込み、③customize_registerでカスタマイザーに設定を追加・transport=postMessageでリアルタイムプレビュー・wp_headでCSSを動的出力、④after_setup_themeでテーマサポートとナビゲーションを宣言・template_includeフィルターでカスタムテンプレートを適用、⑤upgrader_pre_installで更新前に自動バックアップ・after_switch_themeでテーマ切替ログを記録する手順で解決します。

お気軽にご相談ください

お見積りへ お問い合わせへ
▲