[WordPress] 外掛分享: BuddyPig

首頁外掛目錄 › BuddyPig
⚠ 此外掛已下架 — 不再更新維護,建議勿安裝。
全新外掛
安裝啟用
★★★☆☆
3/5 分(2 則評價)
4835 天前
最後更新
問題解決
WordPress 3.4+ v1.1.2 上架:2012-08-12

內容簡介

BuddyPig 是一個遊戲,旨在幫助 BuddyPress 群組成員更好地認識彼此。這個項目是在 Automattic 的一個 Hack Day 活動中開發的。由於我們是一個遠程公司,與某些同事幾個月沒有交談是很常見的。因此,我們想出了 BuddyPig 作為一種有趣的方式來對抗這個問題。

如果你好奇,PIG 是個人資訊遊戲的縮寫。

要使用這個外掛,您需要撰寫一個小型外掛來創建問題,並選擇要使用的 BuddyPress 群組。PIG 可以通過非常易於操作的 Hook 進行自定義,如果有需要的話。下面,我們將會提供一個示範外掛,以設置 PIG。

要運行 PIG,需要事先安裝 BuddyPress 和其活動流特性。此外,還需要 PHP 5.3,略高於 WordPress 的默認需求。

設置示範

您需要告訴 PIG 使用哪些個人資訊項目以及問題的提法。我們提供了一個名為 pig_register_question() 的方程式來完成這些。默認情況下,pig_register_question() 接受兩個引數:BuddyPress 個人資訊欄位名稱以及問題的格式。以下為一個示例:

pig_register_question( 'Address', 'Where does %s live?' );

您會注意到問題格式中的 %s,這是我們插入問題版面時放入的用戶名稱。如果您之前曾經編寫過 PHP,您可能會認出這個語法是後台使用的 sprintf() 函數。

還需要告訴 PIG 要使用哪個 BuddyPress 群組,可以使用 pig_set_group(),並傳遞群組的 ID。以下為一個範例:

pig_set_group( 1 );

pig_register_question() 可以接受另外兩個參數。第一個參數用於指示 PIG 是否在問題中使用名詞所有格。例如,“ Beau出生在哪裡?”不使用名詞所有格,但是“Beau的房子在哪裡?”是使用。如果要使用名詞所有格,則可以將“true”添加為第三個參數,如下所示:

pig_register_question( 'Address', 'Where is %s house?', true );

最後一個可選參數是回撥函數(Callback)。PIG 可以在顯示答案之前為您格式化答案。其中一種使用方法是刪除具有相同意義的重復詞組,例如美國和美利堅合眾國。如果使用這個回撥函數,您還必須確保此功能存在以供 PIG 調用。以下為一個範例:

pig_register_question( 'Address', 'Where is %s house?', 'a8c_dedupe_countries' );
function a8c_pig_dedupe_countries( $country ) {
$usa_versions = array( 'US', 'USA', 'United States', 'U.S.A', 'U.S.', 'America', 'United States of America' );
$uk_versions = array( 'UK', 'U.K.', 'United Kingdom' );
foreach ( $usa_versions as $usa_version ) {
if ( strtolower( $country ) == strtolower( $usa_version ) )
return 'United States';
}

foreach ( $uk_versions as $uk_version ) {
if ( strtolower( $country ) == strtolower( $uk_version ) )
return 'United Kingdom';
}

return $country;
}

您可以將這些設置調用放入自己的外掛中,並像任何其他外掛一樣啟用它。不過,如果您堅持使用主題的 functions.php 文件,也可以將做法套用到主題上。為了確保這些所有數據在 PIG 設置完成後再載入,可以使用 pig_loaded 事件進行回呼(Hook)。

外掛標籤

開發者團隊

⬇ 下載最新版 (v1.1.2) 或搜尋安裝

① 下載 ZIP → 後台「外掛 › 安裝外掛 › 上傳外掛」
② 後台搜尋「BuddyPig」→ 直接安裝(推薦)
📦 歷史版本下載

原文外掛簡介

BuddyPig is a game meant to help BuddyPress group members get to know each other better. It was built during a hack day at Automattic. Since we’re a distributed company, it’s not uncommon to go months without talking to certain colleagues. We came up with BuddyPig as a fun way to work around that.
PIG, in case you’re curious, is an acronym for Personal Information Game.
To use it you’ll have to write a small plugin to create questions and choose the BuddyPress group you’d like to use. PIG is very hook-able, so there’s lots of other stuff you can customize if you’re so inclined. We’ll include an example plugin to setup PIG below.
PIG requires BuddyPress and its Activity Streams feature to be running. It also requires PHP 5.3, which is slightly higher than the default requirement for WordPress.
Example setup
You have to tell PIG what profile items to use and how to ask the questions. We gave you a function called pig_register_question() to do just that. By default, pig_register_question() takes 2 arguments, the BuddyPress profile field name and a format to use to ask the question. Here’s an example:
pig_register_question( 'Address', 'Where does %s live?' );

You’ll notice the %s in the question format, which is where we drop in the user’s name that the question is about. If you’ve written PHP before, you may recognize this syntax from the sprintf() function, which is what is used behind the scenes.
You’ll also have to tell PIG what BuddyPress group to use, which can be done with pig_set_group() by passing the group’s ID. Here’s an example:
pig_set_group( 1 );

pig_register_question() can take two additional arguments. The first one tells PIG whether to make the name possessive when it asks the question. For example, "Where was Beau born?" would not be possessive, but "Where is Beau's house?" would be. If you want PIG to make the name possessive, you can add `true` as the third argument, like this:

pig_register_question( 'Address', 'Where is %s house?', true );

The last optional argument is a callback. PIG can format answers for you before displaying them. One use case of this is deduplicating multiple phrases that mean the same thing, like “United States” and “USA”. If you use this callback, you must also make sure the function exists for PIG to call. Here’s an example:
pig_register_question( 'Address', 'Where is %s house?', 'a8c_dedupe_countries' );
function a8c_pig_dedupe_countries( $country ) {
$usa_versions = array( 'US', 'USA', 'United States', 'U.S.A', 'U.S.', 'America', 'United States of America' );
$uk_versions = array( 'UK', 'U.K.', 'United Kingdom' );
foreach ( $usa_versions as $usa_version ) {
if ( strtolower( $country ) == strtolower( $usa_version ) )
return 'United States';
}

foreach ( $uk_versions as $uk_version ) {
if ( strtolower( $country ) == strtolower( $uk_version ) )
return 'United Kingdom';
}

return $country;
}

You can put these setup calls into your own plugin and activate it just like any other. If you insist, you could also put them into your theme’s functions.php file. To make sure they’re loaded after PIG is setup, you can hook them to the pig_loaded action.

延伸相關外掛

文章
Filter
Apply Filters
Mastodon