PHPインクルード

[ ID : 1039 ]
PHPインクルード

概要

投稿・固定ページ内にPHPファイルをインクルード(挿入/実行)させる方法を紹介。

要件

  • WordPressの投稿・固定ページ内ではphpをそのまま使用できない。
  • ショートコード化して設置。

実装方法


functions.php

function Include_my_php($params = array()) {
    extract(shortcode_atts(array(
        'file' => 'default'
    ), $params));
    ob_start();
    include(get_theme_root() . '/' . get_template() . "/$file.php");
    return ob_get_clean();
}
 
add_shortcode('myphp', 'Include_my_php');

使用箇所

[myphp file='test']

この記事をシェアする

新着記事

全ての記事を見る

KONOCODEのメリット 無料会員登録