問題描述
在 save_post(wp_insert_post()) 結束時,save_post 和 wp_insert_post 動作都被一個接一個地稱為完全相同的參數:wp-includes/post.php(source)
3520 /**
3521 * Fires once a post has been saved.
3522 *
3523 * @since 1.5.0
3524 *
3525 * @param int $post_ID Post ID.
3526 * @param WP_Post $post Post object.
3527 * @param bool $update Whether this is an existing post being updated or not.
3528 */
3529 do_action( 'save_post', $post_ID, $post, $update );
3530
3531 /**
3532 * Fires once a post has been saved.
3533 *
3534 * @since 2.0.0
3535 *
3536 * @param int $post_ID Post ID.
3537 * @param WP_Post $post Post object.
3538 * @param bool $update Whether this is an existing post being updated or not.
3539 */
3540 do_action( 'wp_insert_post', $post_ID, $post, $update );
沒有任何事情發生在他們之間,所以使用一個或另一個似乎沒有區別。
相同的冗餘在 wp_publish_post()(source) 中重複一下,oldest tracked version of the file 也有同樣的兩個動作 (感謝 toscho 指出這些) 。
我錯過了什麼嗎?為什麼他們都在那裏,如果我選擇採取行動,是否有理由選擇一個呢?
最佳解決方案
wp_insert_post was introduced in changeset 2887,並修復 bug #1681 。我找不到 save_post 鈎子的原始來源,但最近才被添加到與 ticket #2063 相關的核心 in changeset 3291 中。顯然,它已經存在於 1.5.2(儘管版本控制 does not support this theory),需要添加回 back-compat 。
顯然,save_post 被添加到 1.5.0,然後在 2.0 開發生命週期中被刪除,然後在 2.0 開發生命週期中加入 wp_insert_post,最後在 2.0 生命週期中加入 save_post,以免破壞 back-compat 。
而不是被棄用的鈎子,你會期望這樣一個鈎子,如果這是真的,它成為開發人員使用的事實上的默認鈎子。
參考文獻
注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。