问题描述
我有一个插件,我希望能够在保存到数据库之前通过一些过滤器来运行该帖子内容。从查看 plugin api,我看到两个看起来像这样可能有帮助的钩子:
save_post
wp_insert_post
唯一的问题是它看起来像 save_post 不需要返回一个变量,所以我不知道如何过滤内容,而 wp_insert_post 看起来文件。
我想做这样的事情:
add_action('whatever_hook_name','my_function');
function my_function($post_content){
return $post_content.' <br> This post was saved on '.time();
}
我会做一些比追加时间戳更有用的东西,即运行一些正则表达式过滤器,但这是我要添加的过滤器/操作的一般类型。
更新
请注意,我想截取数据保存在数据库中的数据,而不是当它显示在帖子中 (例如:不通过向 the_content 添加过滤器)
最佳解决方案
wp_insert_post_data 滤波器可以做到这一点:
add_filter( 'wp_insert_post_data' , 'filter_post_data' , '99', 2 );
function filter_post_data( $data , $postarr ) {
// Change post title
$data['post_title'] .= '_suffix';
return $data;
}
次佳解决方案
使用过滤器 content_save_pre 完全像 the_content – 不同之处在于,当保存文档时,它不会显示。
http://codex.wordpress.org/Plugin_API/Filter_Reference/content_save_pre
参考文献
注:本文内容整合自 Google/Baidu/Bing 辅助翻译的英文资料结果。如果您对结果不满意,可以加入我们改善翻译效果:薇晓朵技术论坛。