WordPress教程

WordPress 允许非管理员用户在评论中插入图片

阿里云

虽然 WordPress 评论系统一直让用户所诟病,但是作为网站的重要组成部分,你又不能完全的放弃它。毕竟这是网站访客/用户与你交流沟通的重要方式。那讲到这里,问题来了:除了基本的文字评论以外,用户可以上传图片评论吗?今天匿名在评论里插入图片的时候,突然发现我插的图片被吞了...尴尬了,原来这么久以来我的插入图片这个按钮功能是“不起作用”的...

然后百度了下,果然有结果了:在 WordPress 中,默认情况下非管理员用户的评论中是不能插入图片的,这是权限设置问题。非管理员权限用户使用该标签会被过滤,因为 img 不在 allow tag 列表里。以下就介绍几种使非管理员用户也能在评论中插入图片的方法,请大家自行取舍~

也想出现在这里?联系我们
创客主机

方法一

打开 wordpress 程序 wp-includes/kses.php 文件,搜索关键词: $allowedtags,大约 419 行,增加对 img 标签的支持,如下:

  1.     $allowedtags = array(
  2.         'a' => array(
  3.             'href' => true,
  4.             'title' => true,
  5.         ),
  6.         'img' => array (
  7.         'src' => array (), 'alt' => array ()
  8.         ),
  9.         'abbr' => array(
  10.             'title' => true,
  11.         ),

注意:这个方法有个弊端,每次升级 wordpress,都必须进行再次的修改!期待高手改进此方法!(已于 2018058 自行解决了,请直接看下面方法 5 的改进版!)

方法二

使用[img]图片 src 地址[/img]来添加评论图片,首先,在 functions.php 中加入下面的代码:

  1. /**
  2. * WordPress中如何允许非管理员用户在评论中插入图片
  3. */
  4. function lxtx_embed_images($content) {
  5.     // $content = preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content);
  6.     $content = preg_replace_callback("/\[img=?\]*(.*?)(\[\/img)?\]/", function($r){ return '<img src="'.$r[1].'" alt="'.basename("$r[1]").'" />'; }, $content);
  7.     return $content;
  8. }
  9. add_filter('comment_text', 'lxtx_embed_images');

然后,使用[img]图片 src 地址[/img]来添加图片当然,为了使用方便贴这个 [img]标签,也可以给评论框那加一个按钮,具体方法如下。先加入下面的这个 js 代码:

  1. function embedImage() {
  2. var URL = prompt('请输入图片 URL 地址:', 'http://');
  3. if (URL) {
  4. document.getElementById('comment').
  5. value = document.getElementById('comment').value + '[img]' + URL + '[/img]';
  6. }
  7. }

再打开主题评论框所在的文件(如:comment.php 等),在适当的位置加入一个“插入图片”按钮:

  1. <a href="javascript:embedImage();">插入图片</a>

方法三

将评论图片地址自动转化为图片,如同煎蛋网一样,将评论的图片地址自动转化为图片。实现方法有 2 种。
① 所有评论图片地址自动转化为图片:

  1. /**
  2.  * WordPress中如何允许非管理员用户在评论中插入图片
  3.  */
  4. add_action('comment_text', 'comments_embed_img', 2);
  5. function comments_embed_img($comment) {
  6.         $size = auto;
  7.         $comment = preg_replace(array('#(http://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#','#(https://([^\s]*)\.(jpg|gif|png|JPG|GIF|PNG))#'),'<img src="$1" alt="" width="'.$size.'" height="" />', $comment);
  8.         return $comment;
  9. }

② 指定特定文章中或所有文章中的评论图片地址自动转化为图片:

  1. /**
  2.  * WordPress中如何允许非管理员用户在评论中插入图片
  3.  */
  4. define('ALLOW_POSTS', '');
  5. function lxtx_fa_comment_image( $comment ) {
  6.     $post_ID = $comment["comment_post_ID"];
  7.     $allow_posts = ALLOW_POSTS ? explode(',', ALLOW_POSTS) : array();
  8.     if(in_array($post_ID,$allow_posts) || empty($allow_posts) ){
  9.         global $allowedtags;
  10.         $content = $comment["comment_content"];
  11.         $content = preg_replace('/(https?:\/\/\S+\.(?:jpg|png|jpeg|gif))+/','<img src="$0" alt="" />',$content);
  12.         $allowedtags['img'] = array('src' => array (), 'alt' => array ());
  13.         $comment["comment_content"] = $content;
  14.     }
  15.     return $comment;
  16. }
  17. add_filter('preprocess_comment', 'lxtx_fa_comment_image');

注:ALLOW_POSTS 里定义的是允许自动贴图的文章或页面的 post_ID ,多篇文章或页面用 , 隔开即可,如需所有文章和页面起效则定义为空即可。

方法四

  1. /**
  2.  * WordPress中如何允许非管理员用户在评论中插入图片
  3.  */
  4. function lxtx_allowedtags_img() {
  5.     global $allowedtags;
  6.     $allowedtags['img'] = array('class'=>true,'src'=>true);
  7.         // $allowedtags['pre'] = array('class'=>array());
  8. }
  9. add_action('comment_post', 'lxtx_allowedtags_img');

方法五

  1. /**
  2. * WordPress中如何允许非管理员用户在评论中插入图片
  3. */
  4. function my_allowed_edittag() {
  5.     define('CUSTOM_TAGS', true);
  6.     global $allowedposttags, $allowedtags;
  7.     $allowedposttags = array(
  8.         'strong' => array(),
  9.         'em' => array(),
  10.         'ol' => array(),
  11.         'li' => array(),
  12.         'u' => array(),
  13.         'ul' => array(),
  14.         'blockquote' => array(),
  15.         'code' => array(),
  16.         'pre' => array(
  17.             'style' => true,
  18.             'class' => true,
  19.         ),
  20.         'a' => array(
  21.         'href' => array (),
  22.         'title' => array ()),
  23.         'img' => array(
  24.         'src' => array ()),
  25.     );
  26.  
  27.     $allowedtags = array(
  28.         'strong' => array(),
  29.         'em' => array(),
  30.         'ol' => array(),
  31.         'li' => array(),
  32.         'u' => array(),
  33.         'ul' => array(),
  34.         'blockquote' => array(),
  35.         'code' => array(),
  36.         'pre' => array(),
  37.         'a' => array(
  38.         'href' => array (),
  39.         'title' => array ()),
  40.         'img' => array(
  41.         'src' => array ()),
  42.     );
  43. }
  44. add_action('init', 'my_allowed_edittag', 10);

注意:这个方法允许的标签全都得自己定义。所以我对该方法进行了改进如下,当然,如果想允许更多 HTML 标签和属性,就请在下面代码中自行添加了:

  1. /**
  2. * WordPress中如何允许非管理员用户在评论中插入图片
  3. */
  4. function lxtx_allowed_html_tags() {
  5.     global $allowedtags;
  6.     $allowedtags['img'] = array(
  7.         'src' => true,
  8.         'alt' => true,
  9.         'class' => true
  10.     );
  11. }
  12. add_action('init', 'lxtx_allowed_html_tags', 10);

WordPress 允许非管理员用户在评论中插入图片

已有 276 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!