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

2016-10-31 06:35 1,563 71 条评论 龙笑天下
Dragon主题购买

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

今天匿名在评论里插入图片的时候,突然发现我插的图片被吞了...尴尬了,原来这么久以来我的插入图片这个按钮功能是“不起作用”的...

然后百度了下,果然有结果了:“在 WordPress 中,默认情况下非管理员用户的评论中是不能插入图片的,这是权限设置问题。非管理员权限用户使用该标签会被过滤,因为 img 不在 allow tag 列表里。”

以下就介绍几种使非管理员用户也能在评论中插入图片的方法,请大家自行取舍~

第一种:修改 wordpress 程序代码

打开 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 的评论框添加“插入图片”按钮

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

第二种:采用标签替换法

使用[img]图片 src 地址[/img]来添加评论图片~ 来自《WordPress 增加评论表情和评论插入图片

首先,在 functions.php 中加入下面的代码:

/**
* WordPress 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
* https://www.ilxtx.com/comment-embed-images.html
*/
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 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
 * https://www.ilxtx.com/comment-embed-images.html
 */
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 评论中嵌入图片 | 知更鸟

此方法可以拓展至其它用途,比如:

WordPress 如何实现文章图片自动替换为指定图片-BG
WordPress 如何实现文章图片自动替换为指定图片

WordPress 如何实现文章图片自动替换为指定图片

很多博友的谷歌广告 Google AdSense 申请不通过,提示“网站不符合合作规范”,而大 Google 却不指出哪些地方不符合规范,这样对于我们广大的站长盆友来说就很蛋疼了,一头雾水,但也只能...

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

/**
 * WordPress 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
 * https://www.ilxtx.com/comment-embed-images.html
 */
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 ,多篇文章或页面用 , 隔开即可,如需所有文章和页面起效则定义为空即可。

添加上述代码后,在发表评论时直接粘贴图片链接地址即可。PS:受此方法启发,写下下面的文章:

如何实现 WordPress 指定文章或页面允许评论昵称/内容带连接地址-BG
如何实现 WordPress 指定文章或页面允许评论昵称/内容带连接地址

如何实现 WordPress 指定文章或页面允许评论昵称/内容带连接地址

今天博客 112 兄提出了一个我认为有点奇怪的需求,也就是本文标题这个啦~~ 下班后立马折腾了下,结果死活不成功... 我是用 is_single() 和 is_page() 来判断的。 然后,...

第四种

/**
 * WordPress 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
 * https://www.ilxtx.com/comment-embed-images.html
 */
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 技巧:为评论模块增加更多 HTML 标签支持

但不知为啥,这种方法在本站,不起作用...

第五种

/**
* WordPress 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
* https://www.ilxtx.com/comment-embed-images.html
*/
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);

摘自《wordpress 过滤发表文章标签处理

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

/**
* WordPress 中如何允许非管理员用户在评论中插入图片 - 龙笑天下
* https://www.ilxtx.com/comment-embed-images.html
*/
function lxtx_allowed_html_tags() {
    global $allowedtags;
    $allowedtags['img'] = array(
        'src' => true,
        'alt' => true,
        'class' => true
    );
}
add_action('init', 'lxtx_allowed_html_tags', 10);

最后,大家也可以试试这个插件,《WordPress 给评论添加图片上传功能的插件:Comment Images》。

「点点赞赏,手留余香」

还没有人赞赏,快来当第一个赞赏的人吧!

声明:本文为原创文章,版权归所有,欢迎分享本文,转载请保留出处!

2016-10-22

2016-11-02

发表评论

表情 格式 贴图 链接 私密 签到
Dragon主题购买阿里云特价云服务器1核2G低至86元,N4共享型服务器3年仅需799元腾讯云特价云服务器1核2G 88元/年 2核4G3M688元/3年,更有千元代金券礼包免费领!
评论
正在努力加载中...
扫一扫二维码分享
×
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept”, you consent to the use of ALL the cookies. Learn more