PHPクラスを作ってみる

functions.php の functionから、別ファイルに書かれたクラスを呼び出す方法を調べてみた。

例:test04_class.phpファイルを作成する。これを functions.phpから呼び出して使いたい。

例:下記のコードを functions.php に書いてみる。

require_once get_theme_file_path() . '/test04_class.php';

add_shortcode('test04', function () {
    $test04 = new Test04();
    return $test04->test04Called();
});

後は、下記のショートコードを投稿ページに貼り付けるだけで良い。

実行結果:

test04 called