WordPress 允许非管理员用户在评论中插入图片
以下代码由PHP站长网 52php.cn收集自互联网现在PHP站长网小编把它分享给大家,仅供参考 虽然 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 的改进版!) 方法二使用 /** * 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'); 然后,使用 } } 再打开主题评论框所在的文件(如: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(), 'pre' => 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); 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |