wordpress中的路徑也不是很負責,有人為了讓wordpress運行速度更快,就直接寫了絕對地址,其實這樣是很不好的,有可能別人修改了wordpress程序的地址,那么這樣你編寫的這個插件或者是主題就只有你自己用,別人無法使用,這樣做得不償失,為了避免錯誤,了解WordPress中與獲取路徑相關的函數很重要。
以下均假設WordPress站點安裝在http://www.uedsc.com下。
站點路徑相關函數
home_url()
返回站點路徑,相當於后台設置->常規中的”站點地址(URL)”。
$url = home_url(); echo $url; //輸出: http://www.uedsc.com $url = home_url('/images/'); echo $url; //輸出:http://www.uedsc.com/images/
site_url()
如果WordPress安裝在域名根目錄下,則該函數與home_url()相同。
如果WordPress安裝在子目錄下,例如http://www.uedsc.com/
,則site_url()
返回WordPress實際安裝地址,相當於后台->設置->常規中的“WordPress 地址(URL)”。
$url = site_url(); echo $url; //假設WordPress安裝在http://www.uedsc.com下 //輸出:http://www.uedsc.com
admin_url()
返回后台地址,傳遞參數后也可返回后台menu的地址
$url = admin_url(); echo $url; //輸出:http://www.uedsc.com/wp-admin/
content_url()
返回實際的wp-content目錄,如果是默認安裝,且裝在根目錄下,則如下所示
$url = content_url(); echo $url; //輸出:http://www.uedsc.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.uedsc.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.uedsc.com/wp-content/uploads
主題路徑相關函數
get_theme_root_uri()
獲取存放主題的目錄URI
echo get_theme_root_uri(); //輸出:http://www.uedsc.com/wp-content/themes
get_theme_root()
獲取存放主題的目錄的服務器絕對路徑
echo get_theme_root(); //輸出:<tt>/home/user/public_html/wp-content/themes</tt>
get_theme_roots()
獲取主題目錄的目錄名稱,如果你的主題目錄是/wp-content/themes
,則
echo get_theme_roots(); //輸出:/themes
get_stylesheet_directory()
獲取當前啟用的主題目錄的服務器絕對路徑,例如
/home/user/public_html/wp-content/themes/twentyeleven
可以用來include文件,例如
<?phpinclude( get_stylesheet_directory() . ‘/includes/myfile.php’); ?>
get_stylesheet_directory_uri()
獲取當前啟用的主題目錄的URI,例如
echo get_stylesheet_directory_uri(); //輸出:http://www.uedsc.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.uedsc.com/wp-content/plugins echo plugins_url('',__FILE__); //輸出:http://www.uedsc.com/wp-content/plugins/myplugin echo plugins_url('js/myscript.js',__FILE__); //輸出:http://www.uedsc.com/wp-content/plugins/myplugin/js/myscript.js
plugin_dir_url()
返回當前插件的目錄URI,例如
echo plugin_dir_url(__FILE__ ); //輸出:http://www.uedsc.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.uedsc.com/wp-content
WP_PLUGIN_DIR
插件目錄的服務器絕對路徑,例如
/home/user/public_html/wp-content/plugins
WP_PLUGIN_URL
插件目錄的URI地址,例如
http://www.uedsc.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目錄。
參考鏈接:
https://developer.wordpress.org/?s=%5Furl&post_type[]=wp-parser-function