<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Horn Network &#187; sidebar</title>
	<atom:link href="http://klcin.tw/net/tag/sidebar/feed" rel="self" type="application/rss+xml" />
	<link>http://klcin.tw/net</link>
	<description>Horn Network (.NET, ASP.NET, C#, VB.NET, JavaScript, Ubuntu, Android ...)</description>
	<lastBuildDate>Fri, 04 Mar 2011 07:25:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>[WP-Plugin]Sidebar Widget 基礎架構（wp-plugin-base.php）</title>
		<link>http://klcin.tw/net/wp-plugin-base-sidebar-widget</link>
		<comments>http://klcin.tw/net/wp-plugin-base-sidebar-widget#comments</comments>
		<pubDate>Wed, 27 Aug 2008 12:37:26 +0000</pubDate>
		<dc:creator>klcintw</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[WP-Plugin]]></category>
		<category><![CDATA[sidebar]]></category>
		<category><![CDATA[widget]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://klcin.tw/net/?p=78</guid>
		<description><![CDATA[本文利用 wp-plugin-base.php 簡單介紹 wordpress plugin 的架構。 前台產生標準 widget 的外觀，內容則是印出設定值。 後台 widget 設定介面，可以修改設定值。 沒有直接（使用 SQL ）變更資料庫。 plugin 基本資料 請參考下圖： 程式碼 &#60;?php /* Plugin Name: widget_name Plugin URI: 網址 Version: 0.1 (版本) Description: 說明文字 Author: klcintw(作者) */ /* 版權宣告 */ /* 常數宣告 */ $_X_WIDGET_NAME = &#34;widget_name&#34;; // 顯示在後台的 widget 名稱 $_X_WIDGET_ID = &#34;widget_id&#34;; // 程式用 widget id [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://klcin.tw/net/wp-content/uploads/2008/08/1.png"><img title="widget" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="155" alt="widget" src="http://klcin.tw/net/wp-content/uploads/2008/08/1-thumb.png" width="176" border="0" style="float:right;"/></a>
<p>本文利用 wp-plugin-base.php 簡單介紹 wordpress plugin 的架構。</p>
<ul>
<li>前台產生標準 widget 的外觀，內容則是印出設定值。</li>
<li>後台 widget 設定介面，可以修改設定值。</li>
<li>沒有直接（使用 SQL ）變更資料庫。</li>
</ul>
<p>  <span id="more-78"></span><br />
<h2>plugin 基本資料</h2>
<p>請參考下圖：</p>
<p align="center"><a href="http://klcin.tw/net/wp-content/uploads/2008/08/snap040.png"><img title="plugin 基本資料" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="262" alt="plugin 基本資料" src="http://klcin.tw/net/wp-content/uploads/2008/08/snap040-thumb.png" width="404" border="0" /></a> </p>
<h2>程式碼</h2>
<p>
<pre class="brush: php">&lt;?php
/*
Plugin Name:    widget_name
Plugin URI:       網址
Version:            0.1 (版本)
Description:     說明文字
Author:             klcintw(作者)
*/
/*  版權宣告 */

/* 常數宣告 */
$_X_WIDGET_NAME = &quot;widget_name&quot;; // 顯示在後台的 widget 名稱
$_X_WIDGET_ID = &quot;widget_id&quot;; // 程式用 widget id
$_X_OPTIONS_KEY = &#039;myWidgetOptions&#039;; // 設定存到資料庫的識別鍵
$_X_OPTIONS_WIDTH = 300; // 後台設定的寬度
$_X_OPTIONS_HEIGHT = 200; // 後台設定的高度
$_X_OPTIONS_DEFAILT[&#039;title&#039;] = &#039;TITLE&#039;;
$_X_OPTIONS_DEFAILT[&#039;int&#039;] = 123;

### Function: 起始Widget
function widget_X_init() {
	if (!function_exists(&#039;register_sidebar_widget&#039;)) return;

	### Function: 功能
	function widget_content($args) {
		// 將 $args 陣列的資訊展開
		extract($args);
		/*  可用 arg 清單：
			$before_widget =&gt; &lt;li id=&quot;widget_name&quot; class=&quot;widget widget_content&quot;&gt;
			$after_widget =&gt; &lt;/li&gt;
			$before_title =&gt; &lt;h2 class=&quot;widgettitle&quot;&gt;
			$after_title =&gt; &lt;/h2&gt;

			$name =&gt; 側邊欄 1
			$id =&gt; sidebar-1
			$widget_id =&gt; widget_name // register_sidebar_widget
			$widget_name =&gt; widget_name // register_sidebar_widget
		*/
		// 取得設定值
		$options = GET_OPTIONS();
		// 輸出前台UI
		echo $before_widget;
		echo $before_title . $options[title] . $after_title;
		echo &#039;&lt;pre&gt;&#039;;
		print_r($options);
		echo &#039;&lt;/pre&gt;&#039;;
		echo $after_widget;
	} // function

	### Function: 設定
	function widget_options() {
		global $_X_OPTIONS_KEY, $_X_WIDGET_ID;
		$options = GET_OPTIONS();

		if ($_POST[$_X_WIDGET_ID.&#039;_is_postback&#039;]) {
			// 讀取新設定值
			$options[&#039;title&#039;] = strip_tags($_POST[$_X_WIDGET_ID.&#039;_title&#039;]);
			$options[&#039;int&#039;] = intval($_POST[$_X_WIDGET_ID.&#039;_int&#039;]);
			update_option($_X_OPTIONS_KEY, $options);
		} // if

		/* 設定畫面 */
		echo &#039;
&lt;p&gt;&lt;label for=&quot;&#039;.$_X_WIDGET_ID.&#039;_title&quot;&gt;標題：&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;&#039;.$_X_WIDGET_ID.&#039;_title&quot; value=&quot;&#039;.$options[&#039;title&#039;].&#039;&quot; /&gt;&lt;/p&gt;
&lt;p&gt;&lt;label for=&quot;&#039;.$_X_WIDGET_ID.&#039;_int&quot;&gt;數字：&lt;/label&gt;&lt;input type=&quot;text&quot; name=&quot;&#039;.$_X_WIDGET_ID.&#039;_int&quot; value=&quot;&#039;.$options[&#039;int&#039;].&#039;&quot; /&gt;&lt;/p&gt;
&lt;input type=&quot;hidden&quot; name=&quot;&#039;.$_X_WIDGET_ID.&#039;_is_postback&quot; id=&quot;is_postback&quot; value=&quot;1&quot; /&gt;
&#039;;
	} // function

	### Helper Function : 取得設定
	function GET_OPTIONS() {
		global $_X_OPTIONS_KEY, $_X_OPTIONS_DEFAILT;
		$options = get_option($_X_OPTIONS_KEY);
		if (!is_array($options)) {
			$options = $_X_OPTIONS_DEFAILT;
		} // if
		return $options;
	}

	/* 註冊 Widget */
	global $_X_WIDGET_NAME,$_X_OPTIONS_WIDTH,$_X_OPTIONS_HEIGHT;
	register_sidebar_widget($_X_WIDGET_NAME, &#039;widget_content&#039;);
	register_widget_control($_X_WIDGET_NAME, &#039;widget_options&#039;,$_X_OPTIONS_WIDTH, $_X_OPTIONS_HEIGHT);

} // function

/* 加入WP系統 */
//add_action(&#039;wp_head&#039;, &#039;AddScriptOrCss&#039;);
add_action(&#039;plugins_loaded&#039;, &#039;widget_X_init&#039;);
?&gt;
</pre>
</p>
<h3>註冊及登記</h3>
<ol>
<li>add_action(<strong>&#8216;plugins_loaded&#8217;</strong>, <strong>&#8216;widget_X_init&#8217;</strong>);      <br />將 widget_X_init 註冊到 WP 系統中，在 plugins_loaded 的事件中呼叫執行。</li>
<li>register_sidebar_widget($_X_WIDGET_NAME, <strong>&#8216;widget_content&#8217;</strong>);      <br />向 WP 系統登記這個 plugin 支援 widget，其內容裡 widget_content 負責處理。</li>
<li>register_widget_control($_X_WIDGET_NAME, <strong>&#8216;widget_options&#8217;</strong>,$_X_OPTIONS_WIDTH, $_X_OPTIONS_HEIGHT);      <br />向 WP 系統登記這個 plugin 的 widget 可以由使用者變更設定值 ，其內容裡 widget_options 負責處理。</li>
</ol>
<h3>其它函式</h3>
<ul>
<li>function widget_content($args)     <br />前台UI呈現</li>
<li>function widget_options()     <br />後台設定</li>
<li>function GET_OPTIONS()     <br />取得設定，若資料庫沒有就傳回預設參數。</li>
</ul>
<h2>重覆使用 wp-plugin-base.php</h2>
<ol>
<li>取代字串「_X_」成為您的新名稱，避免函式名稱重複而發生系統錯誤。</li>
<li>修改 plugin 的基本資料，如作者、說明、版本等。</li>
<li>修改常數宣告的部份以符合您的需求。     <br /><a href="http://klcin.tw/net/wp-content/uploads/2008/08/snap042.png"><img title="常數宣告" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="105" alt="常數宣告" src="http://klcin.tw/net/wp-content/uploads/2008/08/snap042-thumb.png" width="384" border="0" /></a>       <br /><a href="http://klcin.tw/net/wp-content/uploads/2008/08/2.png"><img title="後台設定" style="border-right: 0px; border-top: 0px; border-left: 0px; border-bottom: 0px" height="230" alt="後台設定" src="http://klcin.tw/net/wp-content/uploads/2008/08/2-thumb.png" width="384" border="0" /></a></li>
<li>改寫前台 widget_content </li>
<li>改寫後台 widget_options</li>
</ol>
]]></content:encoded>
			<wfw:commentRss>http://klcin.tw/net/wp-plugin-base-sidebar-widget/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

