Automattic 發表了一個 widgets plugin ,他是可以讓使用者用很簡單的拖拉就可以設定你的 siderbar 所顯示的內容,並且可以選擇順序和是否顯示。這對多人使用的 lyceum 來說是非常方便,就不用再為每個人修改不同的 themes 。也不用為了某個 plugin 去增加任何一行的程式碼。
我就以我上次修改的 Brian’s Latest Comments 為例,所要顯示於 siderbar 的內容,當然就使用原先在 plugin 寫好,我們並不用去修改他,就只要增加 widget 的元件部份的程式碼就可以了。下面是一段的範例:
下载: example.php
- <?php
- function widget_blc_init() {
- // 檢查看看是否啟用 widget plugin
- if ( !function_exists('register_sidebar_widget') )
- return;
- // 這是取一個 widget 名字
- function widget_blc($args) {
- // 這邊是 widget 傳進來的各種參數,通常是 before_widget, before_title,
- // after_widget, 和 after_title 的陣列。
- extract($args);
- // 取得我在設定的內容
- $options = get_option('widget_blc');
- $title = $options['title'];
- $account = $options['account'];
- // 這一行是顯示一個 siderbar 前面的 li 之類的,簡單來說就是 title
- echo $before_widget . $before_title . $title . $after_title;
- // 這一行就是直接呼叫 blc 的內容來顯示。
- blc_latest_comments();
- echo $after_widget;
- }
- // 註冊並在 widget 設定畫面的 title
- register_sidebar_widget('Brian\'s Latest Comments', 'widget_blc');
- // 設定 widget 的畫面大小
- register_widget_control('Brian\'s Latest Comments', 'widget_blc_control', 300, 100);
- }
- // Run our code later in case this loads prior to any required plugins.
- add_action('plugins_loaded', 'widget_blc_init');
- ?>
這樣就可以很簡單的產生一個 widget 了。當然如果你要寫有關設定的話你必須再寫一個 function 。如下所示:
下载: example1.php
- <?php
- function widget_blc_control() {
- $options = get_option('widget_blc');
- if ( !is_array($options) )
- $options = array('title'=>'Msn account', 'account'=>'','show'=>'');
- // 這邊是把你所設定的內容寫入,當然目前我沒有實作他
- if ( $_POST['blc'] ) {
- update_option('widget_blc', $options);
- }
- // Be sure you format your options to be valid HTML attributes.
- $title = htmlspecialchars($options['title'], ENT_QUOTES);
- $buttontext = htmlspecialchars($options['account'], ENT_QUOTES);
- //這邊是顯示你要設定的widget 畫面
- echo '<p style="text-align:right;"><label for="blc-title">Title: <input style="width: 200px;" id="blc-title" name="blc-title" type="text" value="'.$title.'" /></label></p>';
- echo '<input type="hidden" id="blc-submit" name="blc-submit" value="1" />';
- }
- ?>
希望大家能盡量的多作一些有用的 widget 來為民服務,如果有任何想要作成 widget 的 plugin 可以通知我,我可以協助完成。
2 comments
Comments feed for this article
Trackback link
http://blog.twkang.net/2006/04/27/%e5%a6%82%e4%bd%95%e5%af%a6%e4%bd%9c%e4%b8%80%e5%80%8b-widget/trackback/
四月 29, 2006 at 1:23 am
Trackback from roga’s blog - 麻煩的事情...
十一月 25, 2006 at 11:06 pm
Trackback from SILENCE Blog - [Plugin]Post Views Plus Widget...