昨天@流金漩涡问我怎么实现煎蛋的评论直接发图,第一时间想到正则替换,貌似又不太靠谱,最后还是改写到数据库了。
1. add_filter('preprocess_comment')评论提交之后,通过此api来加以处理,比如判断spam。
2. 正则判断是否是图片:"/(http:\/\/|https:\/\/){1}.+?\.(jpg|gif|bmp|bnp|png)/is",不过这个写的还是有点问题。比如333.jpgx.jpg这里会被处理成333.jpg、x.jpg,求正则高手斧正。
3. 普通用户没有发图片(< img src="" />)的权限,如果设置成所有文章评论都可以发图,似乎又不太好,于是加以处理
global $allowedtags; $allowedtags['img'] = array('src' => array (), 'alt' => array ());
以下是完整代码
function auto_comment_image( $comment ) {// by http://mufeng.me $post_ID = $comment["comment_post_ID"]; if($post_ID == 972){ //此处为页面id global $allowedtags; $content = $comment["comment_content"]; // alt部分自行填写 $content = preg_replace('/(http:\/\/|https:\/\/){1}.+?\.(jpg|gif|bmp|bnp|png)/is','<img src="$0" alt="" />',$content); //允许发布img标签 $allowedtags['img'] = array('src' => array (), 'alt' => array ()); // 重新给$comment赋值 $comment["comment_content"] = $content; } return $comment; } add_filter('preprocess_comment', 'auto_comment_image');
PS1:鉴于豆瓣电影已是水军的天下,评分再无参考意义,择日删除账号!
PS2:Greader要在7.1关闭了,实在可惜啊!!
智宇博客
http://mywwwsky.qiniudn.com/wp-content/uploads/2014/10/111_%E6%96%B0.png
zfm
这段代码粘贴在哪里
变成树的梦G
行了~~谢站长