Wordpress用於獲取地址和URL的函式總結
開發wordpress主題或者外掛的時候,最忌諱的一點就是把地址寫死了。這樣的主題或者外掛除了自己能用之外,別人根本就用不了。所以,強烈建議在需要使用地址和URL的場合使用Wordpress自帶的各類用於查詢地址和URL的函式
站點路徑相關函式
home_url()
返回站點路徑,相當於後臺設定->常規中的“站點地址(URL)”。
$url = home_url();
echo $url;
//輸出: http://www.testcom
$url = home_url('/images/');
echo $url;
//輸出:http://www.test.com/images/
site_url()
如果WordPress安裝在域名根目錄下,則該函式與home_url()相同。
如果WordPress安裝在子目錄下,例如http://www.test.com/,則site_url()
返回WordPress實際安裝地址,相當於後臺->設定->常規中的“WordPress 地址(URL)”。
$url = site_url();
echo $url;
//假設WordPress安裝在http://www.test.com下
//輸出:http://www.test.com
admin_url()
返回後臺地址,傳遞引數後也可返回後臺menu的地址
$url = admin_url();
echo $url ;
//輸出:http://www.test.com/wp-admin/
content_url()
返回實際的wp-content目錄,如果是預設安裝,且裝在根目錄下,則如下所示
$url = content_url();
echo $url;
//輸出:http://www.test.com/wp-content
如果在wp-config.php
中改變了wp-content
目錄的位置,則該函式會返回正確地址,例如wp-config.php
中如下定義
define('WP_CONTENT_DIR','/home/user/public_html/cdn');
define('WP_CONTENT_URL' ,'http://sola-cdn.me');
則content_url()的返回值為
http://sola-cdn.me
includes_url()
返回當前WordPress站點存放核心檔案的目錄wp-includes
的地址,可以帶一個$path
作為引數。
$url = includes_url( '/js/');
echo $url;
//輸出:http://www.test.com/wp-includes/js/
wp_upload_dir()
返回WordPress上傳目錄的地址,是一個數組,包含一系列與上傳地址相關的資訊。
<?php $upload_dir = wp_upload_dir(); ?>
提供如下資訊給你
- ‘path’ – 上傳目錄的伺服器絕對路徑,通常以反斜槓(/)開頭
- ‘url’ – 上傳目錄的完整URL
- ‘subdir’ – 子目錄名稱,通常是以年/月形式組織的目錄地址,例如/2012/07
- ‘basedir’ – 上傳目錄的伺服器絕對路徑,不包含子目錄
- ‘baseurl’ – 上傳目錄的完整URL,不包含子目錄
- ‘error’ – 報錯資訊.
例如:
$upload_dir = wp_upload_dir();
echo $upload_dir['baseurl'];
//輸出:http://www.test.com/wp-content/uploads
主題路徑相關函式
get_theme_root_uri()
獲取存放主題的目錄URI
echo get_theme_root_uri();
//輸出:http://www.test.com/wp-content/themes
get_theme_root()
獲取存放主題的目錄的伺服器絕對路徑
echo get_theme_root();
//輸出:<tt>/home/user/public_html/wp-content/themes</tt>
get_theme_roots()
注意啦,僅僅比上面的函式多了一個s。獲取主題目錄的目錄名稱,如果你的主題目錄是/wp-content/themes
,則
echo get_theme_roots();
//輸出:/themes
get_stylesheet_directory()
獲取當前啟用的主題目錄的伺服器絕對路徑,例如
/home/user/public_html/wp-content/themes/twentyeleven
可以用來include檔案,例如
<?php include( get_stylesheet_directory() . ‘/includes/myfile.php’); ?>
get_stylesheet_directory_uri()
獲取當前啟用的主題目錄的URI,例如
echo get_stylesheet_directory_uri();
//輸出:http://www.test.com/wp-content/themes/twentyeleven
可以使用在需要主題目錄URI的場合,例如圖片
<img src="<?php echo get_stylesheet_directory_uri() ?>/images/aternus.png" alt="" title="" width="" height="" />
get_template_directory_uri()
如果當前啟用的主題是一個child theme,該函式返回parent theme的主題目錄URI,用法與get_stylesheet_directory_uri()
類似。
get_template_directory()
如果當前啟用的主題是一個child theme,該函式返回parent theme的主題目錄的伺服器絕對路徑,用法與get_stylesheet_directory()
類似。
get_template()
獲取當前啟用主題的主題目錄名稱,例如現在啟用的主題為twentyeleven,則
echo get_stylesheet();
//輸出:twentyeleven
get_stylesheet()
獲取當前啟用主題的主題目錄名稱,與get_template()
的區別是,如果用了child theme,則返回child theme的目錄名稱。
外掛路徑相關函式
plugins_url()
獲取當前外掛的目錄的URI,例如一個外掛位於/wp-content/plugins/myplugin
下,該目錄下放有外掛的主檔名為myplugin.php
,在myplugin.php
中執行下面的程式碼,結果如下
echo plugins_url();
//輸出:http://www.test.com/wp-content/plugins
echo plugins_url('',__FILE__);
//輸出:http://www.test.com/wp-content/plugins/myplugin
echo plugins_url('js/myscript.js',__FILE__);
//輸出:http://www.test.com/wp-content/plugins/myplugin/js/myscript.js
plugin_dir_url()
返回當前外掛的目錄URI,例如
echo plugin_dir_url(__FILE__ );
//輸出:http://www.test.com/wp-content/plugins/myplugin/
注意結尾有反斜槓。
plugin_dir_path()
返回當前外掛目錄的伺服器絕對路徑,例如
echo plugin_dir_path(__FILE__ );
//輸出:/home/user/public_html/wp-content/plugins/myplugin/
可以用來引用檔案,例如
<?php
define('MYPLUGINNAME_PATH', plugin_dir_path(__FILE__) );
require MYPLUGINNAME_PATH . 'includes/class-metabox.php';
require MYPLUGINNAME_PATH . 'includes/class-widget.php';
?>
plugin_basename()
返回呼叫該函式的外掛檔名稱(包含外掛路徑)
例如在外掛myplugin
下的myplugin.php
檔案中呼叫該函式,結果如下
echo plugin_basename(__FILE__);
//輸出:myplugin/myplugin.php
如果在myplugin/include/test.php
檔案中呼叫(test.php
通過include引用到myplugin.php
中),結果如下
echo plugin_basename(__FILE__);
//輸出:myplugin/include/test.php
路徑相關常量
WordPress中還有一組用define
定義的常量代表路徑。
WP_CONTENT_DIR
wp-content目錄的伺服器絕對路徑,例如
/home/user/public_html/wp-content
WP_CONTENT_URL
wp-content目錄的URI地址,例如
http://www.test.com/wp-content
WP_PLUGIN_DIR
外掛目錄的伺服器絕對路徑,例如
/home/user/public_html/wp-content/plugins
WP_PLUGIN_URL
外掛目錄的URI地址,例如
http://www.test.com/wp-content/plugins
TEMPLATEPATH
當前啟用主題目錄的伺服器絕對路徑,相當於get_template_directory()
例如
/home/user/public_html/wp-content/themes/twentyeleven
STYLESHEETPATH
當前啟用主題目錄的伺服器絕對路徑,相當於get_stylesheet_directory()
,與TEMPLATEPATH
的區別在於如果使用child theme
,該常量指向child theme
目錄。