[WordPress] 各種URL、PATH取得方法まとめ

あ〜あれどうやってとるんだっけ…、がいまだにあるのでまとめた

URL例は、Wordpressのインストールディレクトリを /www/html/wp
サイトURLを http://localhost/ としています。

定数

基本的に定数をテーマやプラグインで直接使用することは推奨されてないが、
Codexの例で結構みかけるので一応載せておく。

URL例 コード 説明
/www/html/wp ABSPATH WordPressのルートディレクトリ
/www/html/wp/wp-content WP_CONTENT_DIR コンテンツディレクトリのフルパス
http://localhost/wp-content WP_CONTENT_URL コンテンツディレクトリのURL
/www/html/wp/wp-content/plugins WP_PLUGIN_DIR プラグインディレクトリのフルパス
http://localhost/wp-content/plugins WP_PLUGIN_URL プラグインディレクトリのURL
/wp-content/uploads UPLOADS マルチサイトのみデフォルトで設定されている。
シングルサイト時は未設定だが自己責任で設定可

関数

各ディレクトリのURL

URL例 コード例 説明
http://localhost/ home_url( ‘/’ ) home_urlは現在のブログのホーム URL (設定の「サイトアドレス」)を取得する
/www/html/wp get_home_path() get_home_path はWordPressルートへのフルパスを取得する
http://localhost/wp site_url() site_urlは現在のブログのWordPress コアファイルが置かれている位置(設定の「WordPress アドレス」)を取得する
http://localhost/wp-admin/ admin_url() admin_urlは現在のサイトの管理領域へのURLを取得する
http://localhost/wp-content content_url() content_urlは現在のサイトの content 領域の URL を取得する
http://localhost/wp-includes/ includes_url() includes_urlは現在のサイトの includes 領域の URL を取得する
http://localhost/wp-content/uploads/2019/03 wp_upload_dir() wp_upload_dirは現在の upload ディクレトリのパスと URL を入れた配列を取得する

array(
'path' => '/www/html/wp/wp-content/uploads/2019/03',
'url' = 'http://localhost/wp-content/uploads/2019/03',
'subdir' => '/2019/03',
'basedir' = '/www/html/wp/wp-content/uploads' ,
'baseurl' => 'http://localhost/wp-content/uploads',
'error' =>false
)
http://localhost/wp-content/themes/sample get_template_directory_uri() get_template_directory_uri は有効化しているテーマのテンプレートディレクトリの URI を取得する
子テーマを使用している場合、親テーマのディレクトリの URI を返す
http://localhost/wp-content/themes/sample get_stylesheet_directory_uri() get_stylesheet_directory_uri は有効化しているテーマのスタイルシート ディレクトリの URI を取得する
子テーマが有効な場合は子テーマのディレクトリを返す
http://localhost/wp-content/themes/sample/style.css get_stylesheet_uri() get_stylesheet_uri は有効化しているテーマのスタイルシート URIを取得する
http://localhost/wp-content/themes get_theme_root_uri() get_theme_root_uri はテーマディレクトリのURIを取得する
/www/html/wp/wp-content/themes get_theme_root() get_theme_root は テーマディレクトリのフルパスを取得する
/themes get_theme_roots() get_theme_roots は template / stylesheetによってキー設定されたテーマディレクトリの名前の配列を取得する
すべてのテーマが同じ親(ルート)ディレクトリを持つ場合はテーマディレクトリの名前を取得する
/www/html/wp/wp-content/themes/sample get_stylesheet_directory() get_stylesheet_directory は現在のテーマまたは子テーマのスタイルシートディレクトリのフルパスを取得する
/www/html/wp/wp-content/themes/sample get_template_directory() get_template_directory は現在のテーマのディレクトリへの絶対パスを取得する
子テーマを使用している場合は、親テーマのディレクトリへの絶対パスを返す

wp_parse_url

URL例 コード例 説明
http wp_parse_url( home_url(), PHP_URL_SCHEME ) スキーマ
localhost wp_parse_url( home_url(), PHP_URL_HOST ) ホスト
/wp-admin/ wp_parse_url( admin_url(), PHP_URL_PATH ) パス

参考

コメントを残す

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください