我想过滤帖子和页面内容,在将内容保存到数据库之前,将html标签附加到特定内容。
例如,给定一个关键字短语"Red Yoga Mats",我想扫描页面/帖子内容,并将"Red Yoga Mats“的第一个实例替换为Red Yoga Mats,将第二个实例替换为Red Yoga Mats,将第三个实例替换为Red Yoga Mats。
这会有多难呢?
(“红色瑜伽垫”这个短语是武断的。我想把它作为一个动态变量传递给执行内容追加的filter函数。)
发布于 2010-10-28 06:07:42
为此,您可以在the_content上使用筛选器。
add_filter( 'the_content', 'highlight_red_yoga_mats' );
function highlight_red_yoga_mats($content) {
// modify $content here
return $content;
}发布于 2010-10-28 06:03:40
如果你在post没有保存,而是显示时(在这种情况下使用the_content filter hack)进行更改,这将会更容易,但如果你真的想在post保存时进行更改,请使用动作hack save_post。一些伪代码:
add_action('save_post', 'my_func');
function my_func($post_ID) {
// retrive content of post based on $post_ID
// use str_replace to replace things
// save post again
}https://stackoverflow.com/questions/4038019
复制相似问题