http://klcin.tw/net/wp-plugin-base-sidebar-widget
本文利用 wp-plugin-base.php 簡單介紹 wordpress plugin 的架構。
- 前台產生標準 widget 的外觀,內容則是印出設定值。
- 後台 widget 設定介面,可以修改設定值。
- 沒有直接(使用 SQL )變更資料庫。
plugin 基本資料
請參考下圖:
程式碼
01.
<?php
02.
/*
03.
Plugin Name: widget_name
04.
Plugin URI: 網址
05.
Version: 0.1 (版本)
06.
Description: 說明文字
07.
Author: klcintw(作者)
08.
*/
09.
/* 版權宣告 */
10.
11.
/* 常數宣告 */
12.
$_X_WIDGET_NAME
=
"widget_name"
;
// 顯示在後台的 widget 名稱
13.
$_X_WIDGET_ID
=
"widget_id"
;
// 程式用 widget id
14.
$_X_OPTIONS_KEY
=
'myWidgetOptions'
;
// 設定存到資料庫的識別鍵
15.
$_X_OPTIONS_WIDTH
= 300;
// 後台設定的寬度
16.
$_X_OPTIONS_HEIGHT
= 200;
// 後台設定的高度
17.
$_X_OPTIONS_DEFAILT
[
'title'
] =
'TITLE'
;
18.
$_X_OPTIONS_DEFAILT
[
'int'
] = 123;
19.
20.
### Function: 起始Widget
21.
function
widget_X_init() {
22.
if
(!function_exists(
'register_sidebar_widget'
))
return
;
23.
24.
### Function: 功能
25.
function
widget_content(
$args
) {
26.
// 將 $args 陣列的資訊展開
27.
extract(
$args
);
28.
/* 可用 arg 清單:
29.
$before_widget => <li id="widget_name" class="widget widget_content">
30.
$after_widget => </li>
31.
$before_title => <h2 class="widgettitle">
32.
$after_title => </h2>
33.
34.
$name => 側邊欄 1
35.
$id => sidebar-1
36.
$widget_id => widget_name // register_sidebar_widget
37.
$widget_name => widget_name // register_sidebar_widget
38.
*/
39.
// 取得設定值
40.
$options
= GET_OPTIONS();
41.
// 輸出前台UI
42.
echo
$before_widget
;
43.
echo
$before_title
.
$options
[title] .
$after_title
;
44.
echo
'<pre>'
;
45.
print_r(
$options
);
46.
echo
'</pre>'
;
47.
echo
$after_widget
;
48.
}
// function
49.
50.
### Function: 設定
51.
function
widget_options() {
52.
global
$_X_OPTIONS_KEY
,
$_X_WIDGET_ID
;
53.
$options
= GET_OPTIONS();
54.
55.
if
(
$_POST
[
$_X_WIDGET_ID
.
'_is_postback'
]) {
56.
// 讀取新設定值
57.
$options
[
'title'
] =
strip_tags
(
$_POST
[
$_X_WIDGET_ID
.
'_title'
]);
58.
$options
[
'int'
] =
intval
(
$_POST
[
$_X_WIDGET_ID
.
'_int'
]);
59.
update_option(
$_X_OPTIONS_KEY
,
$options
);
60.
}
// if
61.
62.
/* 設定畫面 */
63.
echo
'
64.
<p><label
for
=
"'.$_X_WIDGET_ID.'_title"
>標題:</label><input type=
"text"
name=
"'.$_X_WIDGET_ID.'_title"
value=
"'.$options['title'].'"
/></p>
65.
<p><label
for
=
"'.$_X_WIDGET_ID.'_int"
>數字:</label><input type=
"text"
name=
"'.$_X_WIDGET_ID.'_int"
value=
"'.$options['int'].'"
/></p>
66.
<input type=
"hidden"
name=
"'.$_X_WIDGET_ID.'_is_postback"
id=
"is_postback"
value=
"1"
/>
67.
';
68.
}
// function
69.
70.
### Helper Function : 取得設定
71.
function
GET_OPTIONS() {
72.
global
$_X_OPTIONS_KEY
,
$_X_OPTIONS_DEFAILT
;
73.
$options
= get_option(
$_X_OPTIONS_KEY
);
74.
if
(!
is_array
(
$options
)) {
75.
$options
=
$_X_OPTIONS_DEFAILT
;
76.
}
// if
77.
return
$options
;
78.
}
79.
80.
/* 註冊 Widget */
81.
global
$_X_WIDGET_NAME
,
$_X_OPTIONS_WIDTH
,
$_X_OPTIONS_HEIGHT
;
82.
register_sidebar_widget(
$_X_WIDGET_NAME
,
'widget_content'
);
83.
register_widget_control(
$_X_WIDGET_NAME
,
'widget_options'
,
$_X_OPTIONS_WIDTH
,
$_X_OPTIONS_HEIGHT
);
84.
85.
}
// function
86.
87.
/* 加入WP系統 */
88.
//add_action('wp_head', 'AddScriptOrCss');
89.
add_action(
'plugins_loaded'
,
'widget_X_init'
);
90.
?>
註冊及登記
- add_action(‘plugins_loaded’, ‘widget_X_init’);
將 widget_X_init 註冊到 WP 系統中,在 plugins_loaded 的事件中呼叫執行。 - register_sidebar_widget($_X_WIDGET_NAME, ‘widget_content’);
向 WP 系統登記這個 plugin 支援 widget,其內容裡 widget_content 負責處理。 - register_widget_control($_X_WIDGET_NAME, ‘widget_options’,$_X_OPTIONS_WIDTH, $_X_OPTIONS_HEIGHT);
向 WP 系統登記這個 plugin 的 widget 可以由使用者變更設定值 ,其內容裡 widget_options 負責處理。
其它函式
- function widget_content($args)
前台UI呈現 - function widget_options()
後台設定 - function GET_OPTIONS()
取得設定,若資料庫沒有就傳回預設參數。