问题描述

所以,我在我的模板文件 functions.php 中有一个功能,它缓存一个包含自定义分类标准的搜索表单。当我添加/删除/编辑特定分类标签的条款时,我想刷新缓存 (或删除一个缓存组) 。

可能吗?可能用 do_action,但是哪个钩子?谢谢

最佳解决方案

看看 wp-includes/taxonomy.php 。行动是:

do_action( "create_term",       $term_id, $tt_id, $taxonomy );
do_action( "created_term",      $term_id, $tt_id, $taxonomy );
do_action( "edited_term",       $term_id, $tt_id, $taxonomy );
do_action( 'delete_term',       $term,    $tt_id, $taxonomy, $deleted_term );
do_action( "create_$taxonomy",  $term_id, $tt_id );
do_action( "created_$taxonomy", $term_id, $tt_id );
do_action( "edited_$taxonomy",  $term_id, $tt_id );
do_action( "delete_$taxonomy",  $term,    $tt_id, $deleted_term );

次佳解决方案

您正在寻找 created_termedited_termdelete_term 。每个回调接受 3 个参数:

function wpse_created_term( $term_id, $tt_id, $taxonomy ) {
}
function wpse_edited_term( $term_id, $tt_id, $taxonomy ) {
}
function wpse_delete_term( $term_id, $tt_id, $taxonomy ) {
}

add_action( 'created_term', 'wpse_created_term', 10, 3 );
add_action( 'edited_term', 'wpse_edited_term', 10, 3 );
add_action( 'delete_term', 'wpse_delete_term', 10, 3 );

参考文献

注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。