LoginSignup
5
5

More than 5 years have passed since last update.

さくらインターネットの共有 SSL で Contact Form 7 などのテスト環境を構築する

Last updated at Posted at 2012-10-05

あくまでも本番ではなく、テスト環境です。WordPress HTTPS と Contact Form 7 を使って、問い合わせフォームのところだけ HTTPS という感じです。

さくらのレンタルサーバで共有 SSL を使うと、なぜか WordPress の is_ssl()FALSE を返します。PHP が動いてるサーバにはポート 80 でアクセスされるのが原因の模様。$_SERVER からは判断できないようでした。

これでは JS や CSS の URL も http:// がついてしまい、エラーの嵐。is_ssl() に依存しているプラグインのテストもできません。

とりあえずテストするため、テーマの functions.php でフォームを置く URL だけ $_SERVER['HTTPS'] を on にすることにしました。

functions.php
if (stripos($_SERVER['REQUEST_URI'], '/contact') === 0) {
  $_SERVER['HTTPS'] = 'on';
}

それでもまだ Contact Form 7 のローディング画像の URL に http:// がついていました。テンプレートの functions.php が読み込まれる前にプラグインのコードが plugins_url() の結果を変数に持ってしまってたためです。

これはしょうがないので、フィルターで http を https に書き換えることに。

functions.php
if (stripos($_SERVER['REQUEST_URI'], '/contact') === 0) {
  $_SERVER['HTTPS'] = 'on';

  function make_https($url) {
    return preg_replace('/^http:/', 'https:', $url);
  }
  add_filter('wpcf7_ajax_loader', 'make_https');
}

これでなんとか SSL のエラーが消えました。

ただ、これだと本番環境にテーマのファイルをそのままコピーできないので、テーマに入れずにプラグインか何かにしておくとよいかもしれません。

<?php
/*
Plugin Name: SSL Test
Plugin URI: http://somewhere.el.se
Description: Test your WordPress site with shared SSL
Version: 1.0
Author: Shuhei Kagawa
*/

if (stripos($_SERVER['REQUEST_URI'], '/contact') === 0) {
  $_SERVER['HTTPS'] = 'on';

  function make_https($url) {
    return preg_replace('/^http:/', 'https:', $url);
  }
  add_filter('wpcf7_ajax_loader', 'make_https');
}

?>
5
5
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
5
5