LoginSignup
3
5

More than 5 years have passed since last update.

【Wordpress】テーマファイルのURL取得は get_theme_file_uri を、パスにはget_theme_file_pathを。【子テーマ】

Last updated at Posted at 2017-07-24

Wordpressのディレクトリ取得には get_theme_file_uri を使いましょうという話。

子テーマ化する時にハマったのでメモ。

2017年7月時点でググるとまだ、get_template_directory_uri()やら get_stylesheet_directory_uri()やらがバシバシ出て来ますが、子テーマのときは get_stylesheet_directory_uri()て名前おかし過ぎる・・・と思って調べたら4.7から出来てました。

get_theme_file_uri()
https://developer.wordpress.org/reference/functions/get_theme_file_uri/

例えばfunctions.phpにjsファイルを指定する時に、

wp_enqueue_script( 'main-script', get_theme_file_uri() . '/js/main.js', array(), '1.0.0', true);

とか書いておくと、後で子テーマ作る時とかに慌てなくて済みます。今まではget_stylesheet_directory_uri()を使うのだったけれど、多分名前おかしすぎるのでそれっぽい名前の関数が出来たのでしょうね(偏見です)。

で、親テーマのディレクトリが欲しいときはこちらを。
get_parent_theme_file_uri
https://developer.wordpress.org/reference/functions/get_parent_theme_file_uri/

うん、分かりやすくなった!

・・・て言っても中身はget_template_directory_uri()get_stylesheet_directory_uri()ガッツリ使ってあるのでちょっともにょるのですが・・・。wordpressの歴史を感じる瞬間。get_theme_root_uri()から何とかならんかったのかな、と思索しかけて宇宙の彼方に飛んでいきそうだったので止めました。

あと、パスの場合は

get_theme_file_path()
https://developer.wordpress.org/reference/functions/get_theme_file_path/

を使うそうです。

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