WordPress 评论中嵌入图片

来自:互联网
时间:2018-09-18
阅读:

有时发表评论需要添加图片,而WordPress本身并不具备评论贴图功能,可以将下面的代码添加到当前主题functions.php文件中:

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="评论" style="width:'.$size.'; height:'.$size.'" />', $comment);
    return $comment;
}

添加上述代码后,在发表评论时直接粘贴图片链接地址即可。

具体效果请移步:https://www.doukr.net/mm

添加贴图按钮链接

在你的comments.php评论模板样式文件中,添加一个按钮或链接:

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

引入以下js

在你主题的Js文件中引入以下代码:

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

这样你就可以实现在评论中贴图了。

返回顶部
顶部