LoginSignup
9
9

More than 5 years have passed since last update.

[wordpress] ローカルに開発環境構築したら管理画面にログインできないんだけどどうすればいいの

Last updated at Posted at 2015-10-17

何回「ksg」という言葉を叫んだことでしょう。。。

結論

wp-config.php
define('COOKIE_DOMAIN', '');

詳細

ログインできない!!

既に本番環境はあったのですが、mac に WordPress(v3.9) の開発環境を構築しようとした時の事です。
ええ・・・管理画面にログインできないのです。

ログインできない.png

もうね、「お使いのブラウザは Cookie をブロックしているか、Cookie に対応していません。WordPress を使うには Cookie を有効にする必要があります。」の一点張りですよ。

エラーメッセージで探しても、
・ブラウザのコンテンツとキャッシュを削除しろ
・ブラウザの Cookie を有効にして
・ブラウザ変えてやってみて
・plugin を無効化しろ($ mv wp-content/plugins wp-content/_plugins )
functions.php を無効化しろ ($ mv wp-content/theme/xxx/functions.php wp-content/theme/xxx_functions.php )
functions.php 内の空白を削除しろ
DOMAIN_CURRENT_SITE が昔のドメインだった
・ DBユーザのドメインが違う
RELOCATE を設定しろ
・ ログイン画面を再作成しろ(?)
・ IEクソ

などなど・・・試してみるけど、何やってもダメ!全然だめ!!直らない。。。

そこで仕方なくソースから紐解いてみることに。

ソースを読み解く

wp-login.php
 if ( empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) {
   if ( headers_sent() ) {
     $user = new WP_Error( 'test_cookie', sprintf( __( '<strong>ERROR</strong>: Cookies are blocked due to unexpected output. For help, please see <a href="%1$s">this documentation</a> or try     the <a href="%2$s">support forums</a>.' ),
       __( 'http://codex.wordpress.org/Cookies' ), __( 'https://wordpress.org/support/' ) ) );
   } elseif ( isset( $_POST['testcookie'] ) && empty( $_COOKIE[ TEST_COOKIE ] ) ) {
     // If cookies are disabled we can't log in even with a valid user+pass
     $user = new WP_Error( 'test_cookie', sprintf( __( '<strong>ERROR</strong>: Cookies are blocked or not supported by your browser. You must <a href="%s">enable cookies</a> to use WordPress.    ' ),
       __( 'http://codex.wordpress.org/Cookies' ) ) );
   }
 }

エラーから探してみると、どうやらこの wp-login.php$_COOKIE[LOGGED_IN_COOKIE] が空の模様。なるほど。
じゃあこれは何処でセットしてるかというと、wp-includes/pluggable.php の中。

wp-includes/pluggable.php
 setcookie($auth_cookie_name, $auth_cookie, $expire, PLUGINS_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
 setcookie($auth_cookie_name, $auth_cookie, $expire, ADMIN_COOKIE_PATH, COOKIE_DOMAIN, $secure, true);
 setcookie(LOGGED_IN_COOKIE, $logged_in_cookie, $expire, COOKIEPATH, COOKIE_DOMAIN, $secure_logged_in_cookie, true);

ですが、、デバッグしてみた所、どうやら COOKIE はセットできている(サーバ側は成功したと言っている)模様。
setcookie は COOKIE がセットできたら true を返してくれる

実際、response-header を見るとちゃんと Set-Cookie は返ってきている。
なのに、chrome のデベロッパーツール上に COOKIE は作成されていない??これはいかに?

解決

when working on localhost (!) the cookie-domain must be set to "" or NULL or FALSE instead of "localhost"

localhost の COOKIE を設定する時は、COOKIE_DOMAIN""NULL にすればいいらしい。。知らなかった。。情弱ですいませんでした(´・ω・`)

ということで、COOKIE_DOMAINlocalhost から '' にしたら解決。

wp-config.php
define('COOKIE_DOMAIN', '');

これで私の他の WordPress に迷える子羊が何名か救われればいいのですが・・・。

参考(というか試したけど失敗したFAQシリーズ)

https://wpdocs.osdn.jp/%E3%83%AD%E3%82%B0%E3%82%A4%E3%83%B3%E3%81%8C%E3%81%A7%E3%81%8D%E3%81%AA%E3%81%84%E5%A0%B4%E5%90%88
http://nymemo.com/wordpress/664/
http://viral-community.com/wordpress/wordpress-login-5280/
http://sho-tem.com/archives/1398

9
9
2

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
9
9