[WordPress] 外掛分享: Simple Hook Widget

首頁外掛目錄 › Simple Hook Widget
WordPress 外掛 Simple Hook Widget 的封面圖片
10+
安裝啟用
尚無評分
5117 天前
最後更新
問題解決
WordPress 2.8+ v2 上架:2011-12-08

內容簡介

此外掛允許使用者在任何側邊欄中插入一個名為自己選擇的掛鉤(hook)。

該掛鉤可以是任何東西,可以是 WordPress 核心中現有的掛鉤(hook),也可以是外掛、主題或是您臨時想出的東西。一旦掛鉤存在,您的外掛、主題或 WordPress 核心就能夠使用該掛鉤來執行某些動作。

這個掛鉤能夠與其他更複雜的外掛結合使用,讓您能夠在側邊欄中觸發掛鉤(hook),也可以作為製作與主題代碼綁定的非常簡單的小工具的一個快速替代方案。例如,假設您的網站已經存在一個程式碼區塊,您希望它也能夠放置在側邊欄中,但並不想用一個小工具(widget)來包裝它(因為它完全和主題有關),那麼您可以將這個程式碼區塊都掛鉤(hook)到自訂的掛鉤(hook)中,然後使用 Simple Hook Widget 來放置側邊欄中的自訂掛鉤(hook)。

顯然,這並不是小工具(widget)開發的最佳方法,但也許在某些情況下,開發人員需要這樣一個選項。

更新2.0:

新增篩選器,允許開發人員指定可供選擇的掛鉤。

警告:請小心使用此外掛。如果您不是開發人員,且不知道「掛鉤(hooks)」是什麼,請勿使用此外掛。此外掛將允許您輸入任何掛鉤(hook),並在側邊欄中運行它-包括核心 WordPress 中載有可能導致站點問題的動作。

開發者注意事項:

如果未篩選「simple-hook-list」,Simple Hook Widget 將不會在小工具界面中顯示下拉選單,而是提供一個空白的文本框,允許用戶輸入任何他們喜歡的掛鉤(hook)。

在此外掛中使用掛鉤(hooks):

以下是一個簡單的使用 Simple Hook Widget 中的掛鉤(hooks) 的示例。就像在 WordPress 中使用其他掛鉤(hooks) 一樣。請記住,您可以在此處做任何您想做的事情,只需確保在 add_action() 中使用的掛鉤(hook) 名稱和特定小工具中選擇的掛鉤(hook) 名稱相同即可。

您可以通過編寫一個函數(function)並將其添加為掛鉤(hook)的動作來創建任意數量的不同小工具。這裡有一個與第一個示例類似的示例,但這是用來說明這一點的。

在 Simple Hook Widget 中創建預先決定的掛鉤(hooks) 的下拉選單:

版本2.0的 Simple Hook Widget 包括「simple-hook-list」篩選器,允許開發人員指定可從小工具管理面板中選擇的一組掛鉤。

您建立的函數應返回一個關聯數組,其中索引是實際掛鉤(hook)的名稱,值是在小工具面板上表示它的文本。

範例:

```function simple_hook_example_filter($hooks){

$hooks = array (
'example-hook-name-one' => 'Awesome Widget',
'example-hook-name-two' => 'Simple Widget'
);

return $hooks;

}

add_filter('simple-hook-list', 'simple_hook_example_filter');```

外掛標籤

開發者團隊

⬇ 下載最新版 (v2) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「Simple Hook Widget」→ 直接安裝(推薦)

原文外掛簡介

This widget allows the user to insert a hook, with a name of their choosing, in any sidebar.
The hook can be anything, an existing hook from the WordPress Core, a plugin, a theme, or something you’ve come up with on the fly. Once the hook exists, your plugins, your theme, or the WordPress Core to make something happen with that hook.
This can be used in conjunction with other more complex plugins, to allow you to trigger a hook from the sidebar (yes, that is intentionally vague). It can also serve as a quick alternative to making very simple widgets tied to code from a theme. Say you have a chunk of code which already exists on your site, you’d like to also have it placed in a sidebar, but don’t want to make a widget out of it (since its entirely theme-centric). You could simply hook this chunk of code to a custom hook and use the Simple Hook Widget to place that custom hook in the sidebar.
This is clearly not the best method of widget development, but there may be times where such on option is useful to a developer in a pinch.
Update 2.0

New filter which allows developers to specify what hooks are available to choose from.

Warning: Use this plugin with care. If you are not a developer and don’t know what ‘hooks’ are, this plugin is not for you. This plugin will allow you to enter ANY hook, and will run it in the sidebar – that includes core WordPress loaded with actions that could result in problems for your site.
Developer Notes
IMPORTANT:
If the ‘simple-hook-list’ is not filtered, the Simple Hook Widget will not show a drop down in the widget interface, but instead it will provide an empty text field allowing a user to enter ANY hook they like.
Using the hooks
Here is a simple example of how to use a hook used in the Simple Hook Widget. Its just like using any other hook in WordPress. Remember, you can do whatever you want here, you just need to make sure the hook used in the add_action() is the same as the hook name selected in a particular widget.
Example #1:
function simple_hook_super_example() {
echo "This is my super simple hook widget";
}
add_action('example-hook-name-one', 'simple_hook_super_example');

You can create any number of different widgets by simply writing a function, and adding it as an action to a hook. Here is a similar one as the first, but this is here to illustrate the point.

Example #2:

function simple_hook_awesome_example() {
//Do whatever you want your 'widget' to do, when the 'example-hook-name-two' hook is chosen.
echo "This is my awesome simple hook widget";

}
add_action(‘example-hook-name-two’, ‘simple_hook_awesome_example’);
Creating dropdown of Pre-determined hooks
Filter: ‘simple-hook-list’
Version 2.0 of the Simple Hook Widget includes the 'simple-hook-list' filter which allows developers to specify a set of hooks which can be selected from the widgets admin panel.

The function you create should return an associative array where the index is the actual hook's name, and the value is the text that will represent it in the drop down on the widgets panel.

Example:

function simple_hook_example_filter($hooks){
$hooks = array (
‘example-hook-name-one’ => ‘Awesome Widget’,
‘example-hook-name-two’ => ‘Simple Widget’
);
return $hooks;
}
add_filter(‘simple-hook-list’, ‘simple_hook_example_filter’);
IMPORTANT:
If the ‘simple-hook-list’ is not filtered, the Simple Hook Widget will not show a drop down in the widget interface, but instead it will provide an empty text field allowing a user to enter ANY hook they like.
Hook: simple-hook-default
This hook allows developers provide a default value for the Simple Hook widget. By default, the default value for the hook, is an empty string.
Example:

function simple_hook_example_default($default_hook){
return ‘example-hook-name-two’;
}
`
Note: If filtering ‘simple-hook-list’ to create a drop down, the default value must match one of the keys in the array passed to the filter – otherwise this default will do nothing.
Hook: simplehookupdate_
This widget also contains an internal hook, which will be your hook, prefixed with simplehookupdate_. So if you use this plugin to create a hook name ‘testhook’, the widget, aside from creating the ‘testhook’ in the chosen sidebar location, will also create a hook called ‘simplehookupdate_testhook’. This hook occurs within the update method of the WP_Widget class, immediately before $instance is returned.
Not sure how useful this is, but a friend suggested it, so here is it.

延伸相關外掛

文章
Filter
Mastodon