【WordPress】プラグインのパスやURLの取得関数の出力結果出し比べ
WordPressプラグイン開発にあたって、パスやURLの取得に関する関数はいくつかありますが、違いがややこしいので確認した結果をメモしておきます。適宜使い分けませう。
ちなみに戻り値を確認するためのコードを書いたので、無駄にプラグイン化して謎に公開しました。
以下、「hanage_daimajin」というプラグインで各関数の戻り値を表示させた結果です。
なお、ファイル構成はこのようになっております。

plugin_dir_path( __FILE__ )
ファイルが置いてあるディレクトリのサーバーのパスが返ります。
/app/public/wp-content/plugins/hanage_daimajin/
下層ディレクトリの場合
/app/public/wp-content/plugins/hanage_daimajin/test/
plugin_dir_url( __FILE__ )
ファイルが置いてあるディレクトリのURLが返ります。
http://localhost/wp-content/plugins/hanage_daimajin/
下層ディレクトリの場合
http://localhost/wp-content/plugins/hanage_daimajin/test/
plugins_url( __FILE__ )
ファイルのURLが返ります。
http://localhost/wp-content/plugins/hanage_daimajin/index.php
下層ディレクトリの場合
http://localhost/wp-content/plugins/hanage_daimajin/test/test.php
plugins_url()
引数を省略するとプラグインディレクトリのURLが返ります。
http://localhost/wp-content/plugins
下層ディレクトリの場合も同じ
http://localhost/wp-content/plugins/hanage_daimajin
広告
次の記事(2018/10/29)

エックスサーバーにTeraTermでWindowsからSSH接続する
前の記事(2018/10/16)

SVG画像がflexboxやfloat要素内で表示されない場合はサイズ指定が必要