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

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

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

方法一

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

    $allowedtags = array(
        'a' => array(
            'href' => true,
            'title' => true,
        ),
        'img' => array (
        'src' => array (), 'alt' => array ()
        ),
        'abbr' => array(
            'title' => true,
        ),

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

方法二

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

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

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

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

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

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

方法三

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

/**
 * WordPress中如何允许非管理员用户在评论中插入图片
 */
add_action('comment_text', 'comments_embed_img', 2);
function comments_embed_img($comment) {
        $size = auto;
        $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);
        return $comment;
}

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

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

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

方法四

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

方法五

/**
* WordPress中如何允许非管理员用户在评论中插入图片
*/
function my_allowed_edittag() {
    define('CUSTOM_TAGS', true);
    global $allowedposttags, $allowedtags;
    $allowedposttags = array(
        'strong' => array(),
        'em' => array(),
        'ol' => array(),
        'li' => array(),
        'u' => array(),
        'ul' => array(),
        'blockquote' => array(),
        'code' => array(),
        'pre' => array(
            'style' => true,
            'class' => true,
        ),
        'a' => array(
        'href' => array (),
        'title' => array ()),
        'img' => array(
        'src' => array ()),
    );
 
    $allowedtags = array(
        'strong' => array(),
        'em' => array(),
        'ol' => array(),
        'li' => array(),
        'u' => array(),
        'ul' => array(),
        'blockquote' => array(),
        'code' => array(),
        'pre' => array(),
        'a' => array(
        'href' => array (),
        'title' => array ()),
        'img' => array(
        'src' => array ()),
    );
}
add_action('init', 'my_allowed_edittag', 10);

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

/**
* WordPress中如何允许非管理员用户在评论中插入图片
*/
function lxtx_allowed_html_tags() {
    global $allowedtags;
    $allowedtags['img'] = array(
        'src' => true,
        'alt' => true,
        'class' => true
    );
}
add_action('init', 'lxtx_allowed_html_tags', 10);

 

http://xzh.i3geek.com
1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长!
2. 分享目的仅供大家学习和交流,请不要用于商业用途及非法用途,否则后果自负!
3. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
5. 如有链接无法下载、失效或广告,请联系管理员处理!
6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
7. 如遇到加密压缩包,默认解压密码为"qq301.com",如遇到无法解压的请联系管理员!
资源客是一个优秀的分享资源站,本站资源均为各位友友分享而来,特殊原创会标明如有侵犯版权等可联系删除

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