- 赤色のリンクは、まだ日本語Codexに存在しないページ・画像です。英語版と併せてご覧ください。(詳細)
関数リファレンス/plugins url
説明
プラグインのURLを自動で生成してくれる関数。
Retrieves the absolute URL to the plugins or mu-plugins directory (without the trailing slash) or, when using the $path argument, to a specific file under that directory. You can either specify the $path argument as a hardcoded path relative to the plugins or mu-plugins directory, or conveniently pass __FILE__ as the second argument to make the $path relative to the parent directory of the current PHP script file.
使い方
<?php plugins_url( $path, $plugin ); ?>
デフォルトの使い方
<?php $url = plugins_url(); ?>
パラメータ
- $path
- (文字列) (オプション) Path to the plugin file of which URL you want to retrieve, relative to the plugins or mu-plugins directory or to $plugin if specified.
- 初期値: なし
- $plugin
- (文字列) (オプション) Path under the plugins or mu-plugins directory of which parent directory you want the $path to be relative to.
- 初期値: なし
戻り値
- (文字列)
- Absolute URL to the plugins or mu-plugins directory (without the trailing slash) or optionally to a file under that directory.
用例
デフォルトの例
<?php $plugins_url = plugins_url(); ?>
The $plugins_url variable will equal to the absolute URL to the plugins or mu-plugins directory, e.g. "http://www.example.com/wp-content/plugins".
共通の使い方
The plugins_url() function is commonly used in a plugin file. Passing the __FILE__ PHP magic constant in the place of $plugin parameter makes the $path relative to the parent directory of that file:
<?php echo '<img src="' . plugins_url( 'images/wordpress.png', __FILE__ ) . '" > '; ?>
The above might ouput this HTML markup: <img src="http://www.example.com/wp-content/plugins/my-plugin/images/wordpress.png">.
If you are using the plugins_url() function in a file that is nested inside a subdirectory of your plugin directory, you should use PHP's dirname() function:
<?php echo '<img src="' . plugins_url( 'images/wordpress.png', dirname(__FILE__) ) . '" > '; ?>
The above might ouput this HTML markup: <img src="http://www.example.com/wp-content/plugins/images/wordpress.png">.
注
- Uses the WP_PLUGIN_URL or, in the case the $plugin path begins with the WPMU_PLUGIN_DIR path, the WPMU_PLUGIN_URL constant internally, to compose the resultant URL. Note that the direct usage of WordPress internal constants is not recommended.
- Uses apply_filters() to apply "plugins_url" filters on the resultant URL, with the following line of code:
return apply_filters( 'plugins_url', $url, $path, $plugin );
- The plugins_url() function should not be called in the global context of plugins, but rather in a hook like "init" or "admin_init" to ensure that the "plugins_url" filters are already hooked at the time the function is called. This is vital for many site configurations to work, and if plugins_url() is called in the global context of a plugin file it cannot be filtered by other plugins (though mu-plugins are able to filter it because they run before any other plugins).
更新履歴
- 新規導入: 2.6.0
ソースファイル
plugins_url() は wp-includes/link-template.php
にあります。
関連
プラグインのパス: plugins_url(), plugin_dir_url() /en, plugin_dir_path() /en, plugin_basename()
WordPress ディレクトリ: | ||
home_url() | ホーム URL | http://www.example.com |
site_url() | サイトディレクトリ URL | http://www.example.com または http://www.example.com/wordpress |
admin_url() | 管理画面ディレクトリ URL | http://www.example.com/wp-admin |
includes_url() /en | インクルードディレクトリ URL | http://www.example.com/wp-includes |
content_url() /en | コンテンツディレクトリ URL | http://www.example.com/wp-content |
plugins_url() /en | プラグインディレクトリ URL | http://www.example.com/wp-content/plugins |
wp_upload_dir() /en | アップロードディレクトリ URL (配列を返す) | http://www.example.com/wp-content/uploads |
このページ「関数リファレンス/plugins url」は未翻訳です。和訳や日本語情報を加筆してくださる協力者を求めています。
最新英語版: WordPress Codex » Function_Reference/plugins_url (最新版との差分)