通過Typecho的Widget_Options,可以方便地獲取Typecho的系統信息,或者方便地獲取相關配置、資源路徑等。
這裏列出常用的Widget_Options函數和用法,方便各位筒子方便查閱。
通過Widget_Options獲取路徑信息
獲取內置URL
通過這類api,可以獲取TE內置的一些特定URL。
$options = Helper::options();
#1 $options->feedUrl(); //或者 echo $op-feedUrl;
#2 $options->feedRssUrl();
#3 $options->feedAtomUrl();
#4 $options->commentsFeedUrl();
#5 $options->commentsFeedRssUrl();
#6 $options->commentsFeedAtomUrl();
#7 $options->xmlRpcUrl();
#8 $options->loginUrl(); //輸出Typecho的登錄URL地址
#9 $options->registerUrl(); //輸出Typecho的註冊URL地址
#10 $options->registerAction(); //輸出Typecho的註冊Action請求地址
#11 $options->profileUrl(); //輸出Typecho用戶的用戶信息頁面地址
#12 $options->logoutUrl(); //輸出Typecho的註銷地址,用戶點擊之後可實現註銷操作。將輸出:
#1 http://www.4s5.cn/index.php/feed/
#2 http://www.4s5.cn/index.php/feed/rss/
#3 http://www.4s5.cn/index.php/feed/atom/
#4 http://www.4s5.cn/index.php/feed/comments/
#5 http://www.4s5.cn/index.php/feed/rss/comments/
#6 http://www.4s5.cn/index.php/feed/atom/comments/
#7 http://www.4s5.cn/index.php/action/xmlrpc
#8 http://www.4s5.cn/admin/login.php
#9 http://www.4s5.cn/admin/register.php
#10 http://www.4s5.cn/index.php/action/register?_=99788b0bb32d48206a3da981c342a590
#11 http://www.4s5.cn/admin/profile.php
#12 http://www.4s5.cn/index.php/action/logout獲取動態URL
通過這類api,可以動態獲取相對於當前主題、當前插件等的URL。
Widget_Options::siteUrl()
$options->siteUrl(); //或者echo $options->siteUrl
$options->siteUrl('mypage.php');
// http://www.4s5.cn/
// http://www.4s5.cn/mypage.phpsiteUrl()函數將輸出相對於域名的url。如果參數爲空,則輸出網站所在域名,如果參數非空,則輸出相對於/目錄所在的URL。
Widget_Options::index()
$options->index(); //或者echo $options->index
$options->index('mypage.php');
// http://www.4s5.cn/index.php/
// http://www.4s5.cn/index.php/mypage.phpindex函數輸出相對於index.php的url,這個十分常用。如果參數爲空,則輸出index.php,如果參數非空,則輸出相對於index.php所在的url。
注意:如果typecho後臺開啓了僞靜態,那麼Widget_Options::index()將會隱藏url中的index.php。
開啓永久鏈接.png
那麼:
$options->index(); //或者echo $options->index
$options->index('mypage.php');
// http://www.4s5.cn/
// http://www.4s5.cn/mypage.php注意,相對上面,url中少了index.php。
Widget_Options::themeUrl()
$options->themeUrl();
$options->themeUrl('mypage.php');
// http://www.4s5.cn/usr/themes/current_theme/
// http://www.4s5.cn/usr/themes/current_theme/mypage.phpthemeUrl函數將獲取主題模板所在的目錄。如果不帶參數,將獲取當前模板的/路徑,如果參數非空,則獲取相對於當前模板根目錄的路徑。
Widget_Options::pluginUrl()
$options->pluginUrl();
$options->pluginUrl('myplugin/some_script.php');
// http://www.4s5.cn/usr/plugins/
// http://www.4s5.cn/usr/plugins/myplugin/some_script.php
pluginUrl函數和themeUrl類似,不過pluginUrl沒有“當前”插件的概念,即themeUrl會定位到當前啓用的插件目錄,即usr/theme/current_theme/,而pluginUrl僅定位到plugins目錄,即usr/plugins/
Widget_Options::adminUrl()
$options->adminUrl();
$options->adminUrl('mypage.php');
// http://www.4s5.cn/admin/
// http://www.4s5.cn/admin/mypage.phpadminUrl和pluginUrl類似,可以動態定位到admin目錄。
通過Widget_Options獲取系統信息
$options->gmtTime();
//1424352251
$options->software();
//Typecho
$options->version();
//1.0/14.10.10
$options->contentType();
//text/html
echo $options->themeFile('my_theme');
// /path_to_your_site_ducutment/usr/themes/my_theme/
echo $options->themeFile('my_theme','mypage.php');
// /path_to_your_site_ducutment/usr/themes/my_theme/mypage.php
echo $options->pluginDir('TypechoDev');//[注意]
// /path_to_your_site_ducutment/usr/plugins注意,通過閱讀代碼,當前版本(1.0/14.10.10)的Typecho中,pluginDir函數應該有bug,即此函數的參數沒有生效,各位筒子在使用中注意一下。
通過Widget_Options獲取後臺配置
Widget_Options::plugin('TypechoDev')函數,將獲取到TypechoDev的配置。
$plugin_config = $options->plugin('TypechoDev');
echo get_class($plugin_config);
//Typecho_Config假如插件TypechoDev下的Plugin.php文件,存在如下代碼:
public static function config(Typecho_Widget_Helper_Form $form){
$enable_menu = new Typecho_Widget_Helper_Form_Element_Radio(
'enable_menu', array ('0' => '禁用', '1' => '啓用'), '',
':', '');
$form->addInput($enable_menu);
}那麼在模板或者插件代碼中,可以通過如此方式獲取後臺配置值:
$options = Helper::options();
$plugin_config = $options->plugin('TypechoDev');
$plugin_config->enable_menu;//可以獲取到後臺配置的值,即0或者1注意:但要注意,當前版本中,如果對應的插件沒有啓用,那麼可能會導致上述代碼執行失敗,從而導致系統掛掉。