LoginSignup
1
1

More than 3 years have passed since last update.

【Local by Flywheel】WordPressの「致命的なエラーをチェックするためにサイトと通信できないため~」の解決方法【画像あり】

Last updated at Posted at 2020-05-06

備忘録もかねて悩んでいる人もいるのかなと思い投稿してみました。
参考になれば幸いです。

※Windowsの場合で紹介させてもらってます。

エラーの出現

ポートフォリオのWordPress化にあたって、テーマエディターからPHPファイルの編集をしようとしたところ、更新ボタンを押すとこんなエラーが出て変更内容を保存できませんでした。
画像2.png

『致命的なエラーをチェックするためにサイトと通信できないため、PHPの変更は取り消されました。SFTPを使うなど、他の手段でPHPファイルの変更をアップロードする必要があります。』

これはテーマフッターの部分だけでなくPHPのファイルすべてに対して更新ボタンを押すと出てきます。

この記事ではLocal by Flywheelを使ったローカル環境での編集にあたってどうやってこれを解決したのか紹介していきます。

ちなみにスタイルシート(cssファイル)の方は問題なく更新できます。
画像1.png

結論:思ったより簡単に解決できた【画像あり】

解決するのにFTPなどのプロトコルを使用することはありませんでした。

また、修正の仕方は思ったよりもシンプルだったので画像付きで説明します。

解決方法の手順1.

エクスプローラーを開いてローカルディスク(C:)ユーザーuserLocal Sitesを開く

画像4.png
画像5.png
画像6.png

解決方法の手順2.

中にLocal by Flywheelで設定したサイト名が入っています。僕の場合、Portfolioで入れているのでここをクリック。
画像3①.png
 ちなみにこの画面を表示するのにLocal by Flywheelからも入ることができます。
画像3②.png

解決方法の手順3.

apppublicでWordPressのファイルへ移動します。
画像11.png

画像7.png

解決方法の手順4.

次にwp-adminincludesの順で移動しfile.phpを見つけます。
画像8.png
画像9.png
画像10.png

解決方法の手順5.

file.phpをテキストエディタで開いて500行目あたり~610行目あたりまでにある下記のコードをまとめてコメントアウトして保存すれば解決です。

if ( $is_active && 'php' === $extension ) {

    $scrape_key   = md5( rand() );
    $transient    = 'scrape_key_' . $scrape_key;
    $scrape_nonce = strval( rand() );
    // It shouldn't take more than 60 seconds to make the two loopback requests.
    set_transient( $transient, $scrape_nonce, 60 );

    $cookies       = wp_unslash( $_COOKIE );
    $scrape_params = array(
        'wp_scrape_key'   => $scrape_key,
        'wp_scrape_nonce' => $scrape_nonce,
    );
    $headers       = array(
        'Cache-Control' => 'no-cache',
    );

    /** This filter is documented in wp-includes/class-wp-http-streams.php */
    $sslverify = apply_filters( 'https_local_ssl_verify', false );

    // Include Basic auth in loopback requests.
    if ( isset( $_SERVER['PHP_AUTH_USER'] ) && isset( $_SERVER['PHP_AUTH_PW'] ) ) {
        $headers['Authorization'] = 'Basic ' . base64_encode( wp_unslash( $_SERVER['PHP_AUTH_USER'] ) . ':' . wp_unslash( $_SERVER['PHP_AUTH_PW'] ) );
    }

    // Make sure PHP process doesn't die before loopback requests complete.
    set_time_limit( 300 );

    // Time to wait for loopback requests to finish.
    $timeout = 100;

    $needle_start = "###### wp_scraping_result_start:$scrape_key ######";
    $needle_end   = "###### wp_scraping_result_end:$scrape_key ######";

    // Attempt loopback request to editor to see if user just whitescreened themselves.
    if ( $plugin ) {
        $url = add_query_arg( compact( 'plugin', 'file' ), admin_url( 'plugin-editor.php' ) );
    } elseif ( isset( $stylesheet ) ) {
        $url = add_query_arg(
            array(
                'theme' => $stylesheet,
                'file'  => $file,
            ),
            admin_url( 'theme-editor.php' )
        );
    } else {
        $url = admin_url();
    }
    $url                    = add_query_arg( $scrape_params, $url );
    $r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout', 'sslverify' ) );
    $body                   = wp_remote_retrieve_body( $r );
    $scrape_result_position = strpos( $body, $needle_start );

    $loopback_request_failure = array(
        'code'    => 'loopback_request_failed',
        'message' => __( 'Unable to communicate back with site to check for fatal errors, so the PHP change was reverted. You will need to upload your PHP file change by some other means, such as by using SFTP.' ),
    );
    $json_parse_failure       = array(
        'code' => 'json_parse_error',
    );

    $result = null;
    if ( false === $scrape_result_position ) {
        $result = $loopback_request_failure;
    } else {
        $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
        $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
        $result       = json_decode( trim( $error_output ), true );
        if ( empty( $result ) ) {
            $result = $json_parse_failure;
        }
    }

    // Try making request to homepage as well to see if visitors have been whitescreened.
    if ( true === $result ) {
        $url                    = home_url( '/' );
        $url                    = add_query_arg( $scrape_params, $url );
        $r                      = wp_remote_get( $url, compact( 'cookies', 'headers', 'timeout' ) );
        $body                   = wp_remote_retrieve_body( $r );
        $scrape_result_position = strpos( $body, $needle_start );

        if ( false === $scrape_result_position ) {
            $result = $loopback_request_failure;
        } else {
            $error_output = substr( $body, $scrape_result_position + strlen( $needle_start ) );
            $error_output = substr( $error_output, 0, strpos( $error_output, $needle_end ) );
            $result       = json_decode( trim( $error_output ), true );
            if ( empty( $result ) ) {
                $result = $json_parse_failure;
            }
        }
    }

    delete_transient( $transient );

    if ( true !== $result ) {

        // Roll-back file change.
        file_put_contents( $real_file, $previous_content );
        if ( function_exists( 'opcache_invalidate' ) ) {
            opcache_invalidate( $real_file, true );
        }

        if ( ! isset( $result['message'] ) ) {
            $message = __( 'Something went wrong.' );
        } else {
            $message = $result['message'];
            unset( $result['message'] );
        }
        return new WP_Error( 'php_error', $message, $result );
    }
}

ファイルをテキストエディタで見る方法はfile.phpを右クリックして使用しているテキストエディタを選択すればOKです。メモ帳でも修正できますが行がわからないためコメントアウトする場所を見つけにくいかもしれないです。
画像12.png

コメントアウト後

フッター部分のphpファイルを更新すると、
画像13.png
無事更新できたのがわかります。
調べるのは大変でしたが解決してみれば思ったよりシンプルでした。

そもそもなぜエラーが起きるのか

このエラーが出るのはWordPressのバージョンアップで、テーマ保存時の仕組みが変わったことが原因だそうです。
また、今回編集したfile.phpはWordPressのアップデートでまた上書きされてしまうそうなので、その都度編集が必要だそうです。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1