关于wordpress评论添加图片

轩陌

分类: WordPress 5570 6

在做我的这个主题的时候,一直没有注意到只有管理员才能在评论区添加图片,今天在做的时候,也是搜索了很久,找了一些答案都不是很好,还是得自己静下心来看看php的语法,用到了php正则替换符合条件的字符串preg_replace()方法,具体参数:preg_replace (正则表达式, 替换成, 字符串, 最大替换次数【默认-1,无数次】, 替换次数),正则表达式应该各语言大同小异。关于preg_replace()详细讲解>>

在我做图片上传功能的时候,我是采用的[img]https://www.xuanmo.xin/wp-content/themes/xuan/images/comments/20171218233344953759.jpg[/img]这种形式,也是参考别人的,但是正则写的不是很喜欢,所以自己改了一下正则部分。

 

具体实现方法代码如下:


// functions.php
function comments_embed_img($comment) {
  $comment = preg_replace('/\[img\]((http|https):\/\/\S*)\[\/img\]/','<img src="$1" />', $comment);
  return $comment;
}
add_action('comment_text', 'comments_embed_img');

其他方法

这种方法在我这里不起作用,不知道为什么,参考文章>>


function xm_allowedtags_img() {
  global $allowedtags;
  $allowedtags['img'] = array('src'=>true);
}
add_action('comment_post', 'xm_allowedtags_img');
  • 18人 Love
  • 11人 Haha
  • 5人 Wow
  • 1人 Sad
  • 2人 Angry
WordPress、wordpress评论添加图片

作者简介: 轩陌

打赏

生命的意义在于折腾,一直努力成长中,期待梦想实现的那刻。

共 6 条评论关于 “关于wordpress评论添加图片”

Loading...