記事・固定ページ内の任意の位置でPHPファイルをインクルード(挿入/実行)する方法

/wp-content/themes/使用中のテーマ名/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');

読み込みたいphpファイルを作成し、同フォルダにアップする。

記事内に以下を記述(ファイル名がexample.phpの場合)

[myphp file='example']

Copyright © All Rights Reserved · Green Hope Theme by Sivan & schiy · Proudly powered by WordPress