我已经编写了一个过滤器函数,用我网站上传目录中的URL替换帖子中的所有图像URL。但是,当使用content_save_pre
, 过滤它实际上不会工作-但是如果我使用content_edit_pre
过滤器,它工作。这是我的代码:
$oldPostContent = $post->post_content; // pass old content into function as argument
function get_the_post_imgs($oldPostContent) {
global $post;
$newPostContent = $oldPostContent;
$newPostContent = preg_replace_callback(
"/<img.*src=[\\"\']([^\\"\']*)[\\"\'].*>/i", // pattern to match to (i.e the contents of an <img src="..." /> tag)
function ($match) {
return "hello world"; // obviously it\'s a lot more complicated than this but for your sake i\'ve massively simplified it so the src="..." content is just replaced with "hello world" for now
},
$oldPostContent
);
return $newPostContent; // return new manipulated content
}
add_filter(\'content_save_pre\', \'get_the_post_imgs\'); // this doesn\'t work on the content, but if I use content_edit_pre, it does work
在此过程中,我添加了一些评论来帮助您。我很确定正则表达式没有问题(http://regex101.com/r/aP4fU9) 这与我的代码不正常工作有关content_save_pre
需要它来工作。感谢您的帮助:)