[WordPress] 外掛分享: Commentcode API

WordPress 外掛 Commentcode API 的封面圖片。

前言介紹

  • 這款 WordPress 外掛「Commentcode API」是 2016-12-16 上架。
  • 目前尚無安裝啟用數,是個很新的外掛。如有要安裝使用,建議多測試確保功能沒問題!
  • 上一次更新是 2016-12-21,距離現在已有 3056 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 3.4 以上版本才可以安裝。
  • 尚未有人給過這款外掛評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

miunosoft | michael-uno |

外掛標籤

api | shortcode | commentcode | HTML comments |

內容簡介

使用 HTML 註解產生自訂輸出

Commentcode API 讓您可以使用像 HTML 註解一樣的程式碼來產生自訂輸出。

它類似於Shortcode API,不過具有 HTML 註解的形式以及其他一些功能。

由於它採用 HTML 註解的形式,即使使用者停用了您的外掛,嵌入式程式碼也不會顯示,相反地,Shortcode 在停用其外掛時會留在文章中。

不會在文章中留下混亂的痕跡

想像一下,您一直使用一款將特定 Shortcode 轉換為自訂輸出的外掛。後來,您發現另一款外掛更方便,因此卸載了它。

但是,由該外掛使用的 Shortcode 仍然保留在數百篇文章中,手動刪除它們太麻煩,因此您必須請某人運行 SQL 命令。

這是個問題。但是,如果 Shortcode 採用 HTML 註解的形式,它就不會留下這樣的混亂。

語法

它看起來像這樣。

一些輸出。

請注意,不論開頭和結束部分均使用了三個破折號。因此,它幾乎不會與一般的 HTML 註解發生衝突。

支援多維陣列引數

Shortcode 無法將多維引數傳遞給回調函數。

以下屬性將不會被解析。

[my_shortcode foo[1]="one" foo[2]="two"]

然而,Commentcode 可以處理它。

這些屬性被解釋為

array(
'foo' => array(
1 => 'one',
2 => 'two',
)
)

保留大、小寫字元

Shortcode 不允許使用大寫的屬性名稱。

[my_shortcode CapitalName="我需要它使用大寫字元"]

這個屬性會被解釋為

array(
'capitalname' => '我需要它使用大寫字元',
)

當您需要執行需要具有大寫字母的引數名稱的遠端 API 請求時,這是不實用的。

但是,Commentcode API 會保留這些引數名稱。

將是

array(
'CapitalName' => '請保留大小寫!',
)

註冊 Commentcode

使用 add_commentcode() 函數。它接受兩個參數。
1. (string) commentcode 標籤名稱。
2. (callable) 當該標籤名稱的 commentcode 被處理時要調用的回調函數。

回調函數接收三個參數。
1. (string) 經過篩選的文字,通常為空字符串。
2. (array) 在 commentcode 中設置的屬性。
3. (string) commentcode 的標籤名稱。

function get_my_commentcode( $text, $arguments, $tag ) {
return "

" . htmlspecialchars( print_r( $arguments, true ) ) . "
";
}
add_commentcode( 'my_commentcode', 'get_my_commentcode' );

在執行上述代碼的同時,請嘗試在文章中插入

它會產生下列輸出:

Array
(
[Foo] => bar
[numbers] => Array

原文外掛簡介

Generate Custom Outputs with HTML Comments
Commentcode API lets you generate custom outputs with HTML comment-like codes.
It is similar to the Shortcode API except it takes a form of HTML comments and some few features.
Since it takes a form of HTML comments, even the user disables your plugin, the embedded code will not be visible, on contrary to shortcodes that remain in posts when their plugins are deactivated.
Does not leave a mess in posts
Say, you have been using a plugin that converts a plugin specific shortcode into custom outputs. Later you found something else that is more useful and uninstalled it.
But the shortcodes used by that plugin remained in hundreds of posts and it was too much work to manually delete them so you have to ask somebody to run SQL commands.
That’s a problem. What if the shortcode takes a form of an HTML comment? It won’t leave such a mess.
Syntax
It looks like this.

Some outputs.

Notice that tripe dashes are used in the both opening and closing part. So it won’t hardly conflict with generic HTML comments.
Supports Multi-dimensional Array Arguments
The shortcode cannot pass multi-dimensional arguments to the callback function.
The below attributes won’t be parsed.
[my_shortcode foo[1]="one" foo[2]="two"]

However, commentcode can handle it.

The attributes are interpreted as
array(
'foo' => array(
1 => 'one',
2 => 'two',
)
)

Preserved Letter Cases
The shortcode does not allow capitalized attribute names.
[my_shortcode CapitalName="I need it to be capitalized"]

The attribute is interpreted as
array(
'capitalname' => 'I need it to be capitalized',
)

This is not useful when you need to perform remote API requests which require argument names with capital letters.
However, the commentcode API preserves those argument names.

will be
array(
'CapitalName' => 'Please keep capitalization!',
)

Register a Commentcode
Use the add_commentcode() function. It accepts two parameters.
1. (string) the commentcode tag.
2. (callable) a callback function which gets called when the commentcode of the specified tag is processed.
The callback function receives three parameters.
1. (string) The filtered text, usually an empty string.
2. (array) The attributes set in the commentcode.
3. (string) The commentcode tag name.
function get_my_commentcode( $text, $arguments, $tag ) {
return "

" . htmlspecialchars( print_r( $arguments, true ) ) . "

";
}
add_commentcode( 'my_commentcode', 'get_my_commentcode' );

For a test, while running the above code, try inserting in a post.
It will produce this output,
Array
(
[Foo] => bar
[numbers] => Array
(
[1] => one
[2] => two
)

)

各版本下載點

  • 方法一:點下方版本號的連結下載 ZIP 檔案後,登入網站後台左側選單「外掛」的「安裝外掛」,然後選擇上方的「上傳外掛」,把下載回去的 ZIP 外掛打包檔案上傳上去安裝與啟用。
  • 方法二:透過「安裝外掛」的畫面右方搜尋功能,搜尋外掛名稱「Commentcode API」來進行安裝。

(建議使用方法二,確保安裝的版本符合當前運作的 WordPress 環境。


1.0.0 | trunk |

延伸相關外掛(你可能也想知道)

暫無相關外掛推薦。

文章
Filter
Apply Filters
Mastodon