在WP 4.2之前,具有相同s in(例如,標簽和分享s lug“”a a a a a a a a in in in in in in。。。。。。。。)))))。。。。 從WordPress 4.2開始,當這些共享術語中的一個更新時,它將被拆分:更新的術語將被分配一個新的術語ID。
在絕大多數情況下,這種更新將是無縫和平靜的。 然而,一些插件和主題在選項,后期元數據,用戶元數據或其他地方存儲術語ID。 WP 4.2將包括兩種不同的工具來幫助這些插件和主題的作者過渡。
##'split_shared_term'動作
當共享術語被分配一個新的術語ID時,會觸發一個新的“split_shared_term”操作。 存儲術語ID的插件和主題應掛接到此操作以執行必要的遷移。 掛鉤的文檔如下:
```
/**
* Fires after a previously shared taxonomy term is split into two separate terms.
*
* @since 4.2.0
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
```
以下是插件和主題作者如何利用此操作確保存儲的術語ID更新的幾個示例。
##更新存儲在選項中的術語ID
假設你的插件存儲一個名為“featured_tags”的選項,它包含一個術語ID數組(update_option('featured_tags',array(4,6,10)))。 在這個例子中,你將鉤住'split_shared_term',檢查更新后的術語ID是否在數組中,如有必要,進行更新。
```
/**
* Update featured tags when a term gets split.
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function my_featured_tags_split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
// We only care about tags, so we'll first verify that the term is a tag.
if ( 'post_tag' == $taxonomy ) {
// Get the current featured tags.
$featured_tags = get_option( 'featured_tags' );
// If the updated term is in the array, note the array key.
$found_term = array_search( $term_id, $featured_tags );
if ( false !== $found_term ) {
// The updated term is a featured tag! Replace it in the array, and resave.
$featured_tags[ $found_term ] = $new_term_id;
update_option( 'featured_tags', $featured_tags );
}
}
}
add_action( 'split_shared_term', 'my_featured_tags_split_shared_term', 10, 4 );
```
##更新存儲在后期元中的術語ID
有時一個插件可能會在post meta中存儲術語ids。 在這種情況下,使用get_posts()查詢來定位具有元鍵“primary_category”的帖子以及與分割術語ID匹配的元值。 確定帖子后,請使用update_post_meta()更改存儲在數據庫中的值。
```
/**
* Check primary categories when a term gets split to see if any of them
* need to be updated.
*
* @param int $term_id ID of the formerly shared term.
* @param int $new_term_id ID of the new term created for the $term_taxonomy_id.
* @param int $term_taxonomy_id ID for the term_taxonomy row affected by the split.
* @param string $taxonomy Taxonomy for the split term.
*/
function my_primary_category_split_shared_term( $term_id, $new_term_id, $term_taxonomy_id, $taxonomy ) {
// Ignore all updates except those to categories
if ( 'category' == $taxonomy ) {
// Find all the posts where the primary category matches the old term ID.
$post_ids = get_posts( array(
'fields' => 'ids',
'meta_key' => 'primary_category',
'meta_value' => $term_id,
) );
// If we found posts, update the term ID stored in post meta.
if ( $post_ids ) {
foreach ( $post_ids as $post_id ) {
update_post_meta( $post_id, 'primary_category', $new_term_id, $term_id );
}
}
}
}
add_action( 'split_shared_term', 'my_primary_category_split_shared_term', 10, 4 );
```
## `wp_get_split_term()`函數
'split_shared_term'是處理術語ID更改的首選方法。 但是,可能有些情況 - 例如延遲插件更新 - 哪些術語被拆分,沒有插件有機會鉤住'split_shared_term'動作。 WP 4.2存儲有關已分裂的分類術語的信息,并提供wp_get_split_term()實用程序函數,以幫助開發人員檢索此信息。
考慮上面的情況,您的插件在名為“featured_tags”的選項中存儲術語ID。 您可能需要構建一個驗證這些標記ID的功能(可能要在插件更新中運行),以確保沒有任何功能標簽已被拆分:
```
function my_featured_tags_check_for_split_terms() {
$featured_tag_ids = get_option( 'featured_tags', array() );
// Check to see whether any IDs correspond to post_tag terms that have been split.
foreach ( $featured_tag_ids as $index => $featured_tag_id ) {
$new_term_id = wp_get_split_term( $featured_tag_id, 'post_tag' );
if ( $new_term_id ) {
$featured_tag_ids[ $index ] = $new_term_id;
}
}
// Resave.
update_option( 'featured_tags', $featured_tag_ids );
}
```
>[info] 請注意,wp_get_split_term()具有兩個參數 - $ old_term_id和$ taxonomy - 并返回一個整數。 如果您需要檢索與舊術語ID相關聯的所有拆分術語的列表,無論分類如何,請使用wp_get_split_terms($ old_term_id)。
- 簡介
- 主題開發
- WordPress許可證
- 什么是主題
- 開發環境
- 主題開發示例
- 主題基礎
- 模板文件
- 主樣式表(style.css)
- 文章類型
- 規劃主題文件
- 模板層級
- 模板標簽
- 循環
- 主題函數
- 連接主題文件和目錄
- 使用CSS和JavaScript
- 條件標簽
- 類別,標簽和自定義分類
- 模板文件
- 內容模板文件
- 頁面模板文件
- 附件模板文件
- 自定義內容類型
- 部分和其他模板文件
- 評論模板
- 分類模板
- 404頁面
- 主題功能
- 核心支持的功能
- 管理菜單
- 自定義Headers
- 自定義Logo
- 文章格式
- 置頂文章
- Sidebars
- Widgets
- 導航菜單
- 分頁
- 媒體
- Audio
- Images
- Galleries
- Video
- 精選圖片和縮略圖
- 國際化
- 本地化
- 輔助功能
- 主題選項 – 自定義API
- 定制對象
- 改進用戶體驗的工具
- 定制JavaScript API
- JavaScript / Underscore.js渲染的自定義控件
- 高級用法
- 主題安全
- 數據消毒/逃避
- 數據驗證
- 使用隨機數
- 常見漏洞
- 高級主題
- 子主題
- UI最佳實踐
- JavaScript最佳做法
- 主題單元測試
- 驗證你的主題
- Plugin API Hooks
- 發布你的主題
- 所需的主題文件
- 測試
- 主題評論指南
- 寫文檔
- 提交你的主題到WordPress.org
- 參考文獻
- 模板標簽列表
- 條件標簽列表
- 編碼標準
- HTML編碼標準
- CSS編碼標準
- JavaScript編碼標準
- PHP編碼標準
- 插件開發
- 插件開發簡介
- 什么是插件
- 插件基礎
- 頭部要求
- 包括軟件許可證
- 啟用 / 停用 Hooks
- 卸載方法
- 最佳做法
- 插件安全
- 檢查用戶功能
- 數據驗證
- 保護輸入
- 保護輸出
- 隨機數
- Hooks
- Actions
- Filters
- 自定義Hooks
- 高級主題
- 管理菜單
- 頂級菜單
- 子菜單
- 短代碼
- 基本短碼
- 封閉短碼
- 帶參數的短代碼
- TinyMCE增強型短碼
- 設置
- 設置API
- 使用設置API
- 選項API
- 自定義設置頁面
- 元數據
- 管理帖子元數據
- 自定義元數據
- 渲染元數據
- 自定義文章類型
- 注冊自定義文章類型
- 使用自定義文章類型
- 分類
- 使用自定義分類
- 在WP 4.2+中使用“split術語”
- 用戶
- 創建和管理用戶
- 使用用戶元數據
- 角色和功能
- HTTP API
- JavaScript
- jQuery
- Ajax
- 服務器端PHP和入隊
- Heartbeat API
- 概要
- 計劃任務
- 了解WP-Cron計劃
- 安排WP-Cron 事件
- 將WP-Cron掛接到系統任務計劃程序中
- WP-Cron簡單測試
- 國際化
- 本地化
- 如何國際化您的插件
- 國際化安全
- WordPress.org
- 詳細插件指南
- 規劃您的插件
- 如何使用Subversion
- 插件開發者常見問題
- 開發工具
- Debug Bar 和附加組件
- 輔助插件
- REST API手冊
- 資源
- 文章
- 文章修訂
- 文章類型
- 文章狀態
- 類別
- 標簽
- 頁面
- 評論
- 分類
- 媒體
- 用戶
- 設置
- 使用REST API
- 全局參數
- 分頁
- 鏈接和嵌入
- 發現
- 認證
- 經常問的問題
- 骨干JavaScript客戶端
- 客戶端庫
- 擴展REST API
- 添加自定義端點
- 自定義內容類型
- 修改回應
- 模式
- 詞匯表
- 路由和端點
- 控制器類