WP默认非管理员在发表评论时无法使用 img 标签,特别不美,两种方法修改
一、修改主题法 把下面的代码加到主题的 functions.php 就可以了:
function sig_allowed_html_tags_in_comments() {
define('CUSTOM_TAGS', true);
global $allowedtags;
$allowedtags = array(
'img'=> array(
'alt' => true,
'class' => true,
'height'=> true,
'src' => true,
'width' => true,
),
);
}
add_action('init', 'sig_allowed_html_tags_in_comments', 10);二、修改核心代码 找到
wp-includes/kses.php文件,搜索?$allowedtags = array(,大概 424 行,在下面添加:
'img' => array(另外,下面是让 WP 识别评论区图片链接的代码,加到'src' => true, 'alt' => true, 'class' => true, 'height'=> true, 'width' => true,),
functions.php:
add_action('comment_text', 'comments_embed_img', 2);
function comments_embed_img($comment) {$size = auto;
$comment = preg_replace(array('#(https://([^\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;
}
上面两种方法选择一种就即可。
hdjabihbwk Lv.1
10月7日
新车新盘 嘎嘎稳 嘎嘎靠谱