テーマのfunctions.phpを?>で終わらせるとエラーになる可能性があります

WordPressのログイン画面で以下の様なメッセージが表示され、ログイン出来ない状態に遭遇した。

エラー: 予期しない出力により Cookie がブロックされました。ヘルプが必要な場合はこちらのドキュメンテーションを参照するか、サポートフォーラムをご利用ください。

WordPressをデバッグモードにすると次のメッセージが表示された。

Notice: is_page was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/users/web/wp-includes/functions.php on line 5535

Notice: is_single was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in /home/users/web/wp-includes/functions.php on line 5535

Warning: Cannot modify header information – headers already sent by (output started at /home/users/web/wp-includes/functions.php:5535) in /home/users/web/wp-includes/pluggable.php on line 1340

Warning: Cannot modify header information – headers already sent by (output started at /home/users/web/wp-includes/functions.php:5535) in /home/users/web/wp-includes/pluggable.php on line 1343

警告などのメッセージだが、対象のファイルはWordPressのコアファイルだ。
コアファイルを編集した覚えは無い。

エラーが発生する前、最後に編集したのは使用しているテーマのfunctions.phpだ。

<?php

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('style')
    );
}

?>

使用しているテーマは子テーマだ。
そのためfunctions.phpはとても簡素。
コード自体に誤りは無い。

但し、ファイルの内容の最後が問題だった。

次の様に修正した。

<?php

add_action( 'wp_enqueue_scripts', 'theme_enqueue_styles' );
function theme_enqueue_styles() {
    wp_enqueue_style( 'style', get_template_directory_uri() . '/style.css' );
    wp_enqueue_style( 'child-style',
        get_stylesheet_directory_uri() . '/style.css',
        array('style')
    );
}

変更した箇所はお分かりだろうか?

最後の?>を取り除いただけだ。

これで先ほどのログイン画面でのエラーは無くなり、無事にログイン出来るようになった。

やーまん
  • WordPressでテーマやプラグインの開発をしています。
    開発中に使用したHow toを中心に書き残していきます。

WordPress

コメントする

メールアドレスが公開されることはありません。 が付いている欄は必須項目です