LoginSignup
2
3

More than 3 years have passed since last update.

[WordPress]includeしたPHPのソースがブラウザに表示されてしまう

Last updated at Posted at 2019-08-29

実施環境

  • Windows10
  • xampp
  • WordPress 5.2.2
  • PHP 7.3.7

経緯

独自テンプレート内のindex.phpでhoge.phpをincludeしたところ
hoge.php内のechoで記述したhtmlを認識してしまい、ブラウザ上にPHPのソースが表示されてしまいました。

page-xxx.php
<?php
/**
 * 
 * Template Name:サンプルテンプレート
 * 
 */
get_header();
?>
<?php
  include __DIR__.'index.php';
?>
<?php
  get_footer();
?>
hoge.php
<?php
  function strOutput(){
    echo '<p>hogehoge!</p>'
  }
?>
index.php
<?php
  include __DIR__.'hoge.php';
?>
  <body>
    <div>
      <?php strOutput(); ?>
    </div>
  </body>

解決策

.phpの拡張子を持つファイルをPHPファイルとして認識してもらうためにApacheの設定を修正しました。
xamppがインストール済だと以下に設定ファイルが配置してあります。

C:\\xampp\\apache\\conf\\httpd.conf

ファイルを開き、最下部に以下を追記し保存。

AddType application/x-httpd-php .php
AddType application/x-httpd-php-source .phps

Apacheを再起動します。

参考資料

参考にさせていただきました!
http://www.8acrewood.com/wordpress/archives/249

2
3
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
2
3