[WordPress] 外掛分享: Select 2 Post Selector

前言介紹

  • 這款 WordPress 外掛「Select 2 Post Selector」是 2015-01-30 上架。 目前已經下架不再更新,不建議安裝使用。
  • 目前有 80 個安裝啟用數。
  • 上一次更新是 2019-04-22,距離現在已有 2204 天。超過一年沒更新,安裝要確認版本是否可用。以及後續維護問題!
  • 外掛最低要求 WordPress 4.0 以上版本才可以安裝。
  • 有 3 人給過評分。
  • 還沒有人在論壇上發問,可能目前使用數不多,還沒有什麼大問題。

外掛協作開發者

magicroundabout |

外掛標籤

posts | select2 | relationships | custom post types |

內容簡介

本外掛提供一個程式庫,供開發者使用以建立 AJAX 引擎的下拉式文章選擇框。例如,假如您有一個外掛或主題,建立了一個自訂文章型別稱為「資源」,您希望使用者能在每篇文章中手動地建立「相關文章」和「相關資源」,使用含有資料的元件 (相關文章/資源的 ID) 並儲存至自訂欄位中。其中一種方法是使用選擇框,但是這種方式不太友善,尤其是當有大量的文章和資源時,效能會受到影響。此外,這種方式甚至可能會超過 PHP 的記憶體限制並導致網站管理員崩潰。此外掛能增進可選擇文章列表的使用者體驗和效能,透過增強 AJAX 引擎的 Select2 選擇框功能。此外掛會:提供簡單的方式以部分、不區分大小寫的文章標題搜尋法,挑選欲從某文章型別所選擇的文章將選擇的文章 ID 儲存至自訂文章變數中在您的外掛或主題中只需要少量的撰寫程式碼(需多少依照情況決定)。Select2的授權為 MIT,版權所有 (c) 2012-2015 Kevin Brown、Igor Vaynberg 和 Select2 貢獻者。
設定
本外掛提供您用於創建主題和外掛的程式庫。使用前,您必須在您的主題或外掛中新增程式碼,需要新增一些程式碼。基本的步驟有:初始化文章選擇框創建顯示在自訂文章型別編輯頁面的元件框將文章選擇框放入元件框內取得您的數據。請注意:所有程式碼都以“demo”為前綴詞-請更改為您自己的專案相應的前綴詞,或將呼叫方法放置在特定類別或命名空間中。
初始化文章選擇框
您應在 admin_init 時初始化文章選擇框,並為您想要創建的每個文章選擇框呼叫 S2PS_Post_Select::create()。create 方法的參數為:$field_id - 'name' of the field - 用於識別打印或保存該欄位的唯一標識符 - 對於您的文章選擇,它必須是唯一的$meta_key - 要從/到其中取得/保存數據的 meta_key$form_field_name - 要創建的選擇表單欄位的名稱屬性$form_field_label - 用於選擇表單欄位的標籤文字$post_post_type - 您希望為其顯示選擇框的文章的 post type$item_post_type - 應在選擇列表中顯示的項目類型$additional_query_params - 創建列表的任何其他查詢參數(如果您想過濾顯示於選擇器中的項目)。這是您會傳遞給 WP_Query 的參數陣列 - 您可以傳遞幾乎任何東西,除了 post_type。大概不推薦使用類似分頁參數之類的東西。。在顯示文章選擇框的程式碼中需要使用第一個參數,$field_id,所以記住它們。
這是初始化「相關文章」和「相關資源」所需的程式碼節錄 :```S2PS_Post_Select::create( 'demo_related_posts', 'related_posts', 'demo_related_posts', 'Related Posts', 'post', 'post', array() );```
取得您的數據
在 CSRF 攻擊防護機制下,我們不再能使用 $_POST 取得已儲存的同步資料。如果您想要取得自訂文章型別中已選擇的文章,您需要手動從 $POST 項目中解析結果。如果您已經使用「自訂元件樣板」(CMB2) 構建您的元件框,那麼這個問題就已解決。
```Post_Select::select_field_save( $_POST['demo_related_posts'], 'demo_related_posts', $post_id );```

原文外掛簡介

This plugin provides a library for developers to use to create AJAX-powered drop-down post select boxes.
This is best explained with an example. Say you have a plugin or theme that sets up a custom post type called “Resources”. You want users to be able to manually create, for each post, “Related posts” and “Related resources” using meta boxes with data (the IDs of the related posts/resources) saved to custom fields.
One way to do this is to use a select box. This would be a big long list of ALL the posts or resources on your site. This is not very user friendly, but also, with a large number of posts and resources to list, can be a real performance hog. I’ve even had this approach blow the PHP memory_limit and break the admin of sites.
This plugin improves both the user experience and performance of creating selectable post lists by enhancing them with the functionality of AJAX-powered Select2 select boxes.
The plugin:

gives a simple way to select posts from a specified post type based on a partial, case-insensitive title search
saves selected post IDs to custom post meta variables
requires minimal coding (but, sadly, does require some coding in your plugin or theme)

Select2 is MIT Licensed and Copyright (c) 2012-2015 Kevin Brown, Igor Vaynberg, and Select2 contributors,
Configuration
This plugin provides a library for you to use in Themes and Plugins that you create. To use it you will need to add some code to your plugin or theme. Not much, but you will need to add some.
The basic steps are:

initialise the post selectors
create a meta box that displays on your custom post type’s edit page
populate the meta box with the post selectors
getting your data out

Note: all code here is prefixed with ‘demo‘ – please change this to an appropriate prefix for your own project or put the calls in a wrapper class or namespace of some sort._
Initialise Post Selectors
You should initialise the Post Selectors on admin_init – they aren’t required in the front end.
You should call S2PS_Post_Select::create() for each post selector that you want to create.
The parameters to the create method are:

$field_id – this is the ‘name’ of the field – used to identify it for printing or saving – it must be unique for your post selector
$meta_key – the meta_key to fetch/save data to/from
$form_field_name – the name attribute of the select form field to be created
$form_field_label – the label text for the select form field
$post_post_type – the post type of the posts which we want the select box to appear for
$item_post_type – the post type of the things to appear in the select list
$additional_query_params – any additional query params for generating the list (if you want to filter what appears in the selector); this is an array of parameters that you would pass to WP_Query – you can pass pretty much anything, I think, except post_type. Using things like pagination parameters probably isn’t recommended.

The first parameter, $field_id, is needed when adding the code to display the post selector, so remember what they are.
Here’s some code that initialises “Related posts” and “Related resources” (of “resources” custom post type) to be displayed on the edit screen of posts.
add_action('admin_init', 'demo_create_post_selects');
function demo_create_post_selects() {
S2PS_Post_Select::create( 'related-articles', 'related-articles', 'demo_related_articles', 'Related articles', 'post', 'post');
S2PS_Post_Select::create( 'related-resources', 'related-resources', 'demo_related_resources', 'Related resources', 'post', 'resources' );
}

Create meta boxes
This just uses a standard WordPress add_meta_box call to add a meta box to the post type that you want to display the selectors in. The callback to print the content of the meta box (demo_print_related_items_meta_box) will use the code in the next section to display the post selectors.
add_action('add_meta_boxes', 'demo_add_related_items_meta_box');
function demo_add_related_items_meta_box() {
add_meta_box( 'related_items', 'Related Items', 'demo_print_related_items_meta_box', 'post', 'normal',
'default' );
}

Add post selectors to meta boxes
This is the callback that is called to display the contents of the meta box. Here we need out $field_id‘s from the first section of code.
function demo_print_related_items_meta_box( $post ) {
S2PS_Post_Select::display( 'related-articles' );
S2PS_Post_Select::display( 'related-resources' );
}

Getting data out
Data is stored as regular post meta, so you can retrieve it with get_post_meta() calls (or other functions that get post meta for you).
One important thing to note is that if multiple posts are selected in a given field then these are stored as multiple post meta entries with the same key. So, when you call get_post_meta(), ensure that the $single parameter is set to false (it is by default).

各版本下載點

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

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


最新版本

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

  • WooCommerce variations Select2 》這個外掛允許 WooCommerce 商店老闆將商品屬性下拉式清單的原生設計改成自訂的外觀,看起來在所有瀏覽器和平台上都相同。, 這個外掛使用了隨附 WooCommerce ...。
  • Customize Object Selector 》此外掛提供自訂器(Customizer)控制項,以選擇一或多個文章(以及最終的專案和使用者選擇)。, 一直以來,核心版本擁有一個 dropdown-pages 控制類型,這個...。

文章
Filter
Mastodon