我经常使用高级自定义字段为我的客户创建很棒的后端界面(我相信我们很多人都是这样做的…)。ACF包括一个选项附加组件,它创建一个或多个全局选项页面,用户可以从任何地方提取数据。例如,我使用选项页面让客户选择要在主页旋转木马上显示的5篇文章。
我开始使用选项加载项和超级缓存遇到问题。似乎默认情况下,保存选项页面对缓存没有影响,例如主页旋转木马保持不变,客户端感到困惑…
我需要挂接到选项页,以便在保存选项页时以编程方式清空缓存,因为我通常不会给我的客户端管理员访问权限,也不会用缓存之类的技术来打扰他们。
ACF的作者说:There is an action called acf/save_post which is used to save all the posted field data. Perhaps you could use this action to hook into and flush the cache.
You could use the $post_id parameter to decide whether it is an options page or not. I believe the options page will pass through \'0\' as the post_id. Either that or \'options\'.
是否有人能够帮助创建一个操作,该操作将在每次保存选项页时完全清除超级缓存缓存。这无疑会帮助很多人!
acf/save_post info here: http://www.advancedcustomfields.com/resources/actions/acfsave_post/
提前非常感谢。
最合适的回答,由SO网友:Jacob 整理而成
此功能将在保存ACF选项页面时清除WP超级缓存。享受
<?php
/* Additional Function to prune the Cache if $post_id is \'0\' or \'options\' */
function f711_clear_custom_cache($post_id) {
// just execute if the $post_id has either of these Values. Skip on Autosave
if ( ( $post_id == 0 || $post_id == \'options\' ) && !defined( \'DOING_AUTOSAVE\' ) ) {
// Some Super Cache Stuff
global $blog_cache_dir;
// Execute the Super Cache clearing, taken from original wp_cache_post_edit.php
if ( $wp_cache_object_cache ) {
reset_oc_version();
} else {
// Clear the cache. Problem: Due to the combination of different Posts used for the Slider, we have to clear the global Cache. Could result in Performance Issues due to high Server Load while deleting and creating the cache again.
prune_super_cache( $blog_cache_dir, true );
prune_super_cache( get_supercache_dir(), true );
}
}
return $post_id;
}
// Add the new Function to the \'acf/save_post\' Hook. I Use Priority 1 in this case, to be sure to execute the Function
add_action(\'acf/save_post\', \'f711_clear_custom_cache\', 1);
?>