Home  >  Article  >  php教程  >  wordpress文章只允许管理员评论

wordpress文章只允许管理员评论

WBOY
WBOYOriginal
2016-06-06 20:08:35925browse

是文章,不是页面,跟公告栏不是一回事! 题目的意思可以理解成把"关闭评论"改成"关闭游客评论". 我们知道在编辑文章的时候,有关闭评论的选项,其实这个选项本身就很蛋疼,应该换成"关闭游客评论",今天有人提到这个问题,所以顺便写出来,因为没有别的可写了. 折腾

是文章,不是页面,跟公告栏不是一回事!
题目的意思可以理解成把"关闭评论"改成"关闭游客评论".
我们知道在编辑文章的时候,有关闭评论的选项,其实这个选项本身就很蛋疼,应该换成"关闭游客评论",今天有人提到这个问题,所以顺便写出来,因为没有别的可写了.
折腾过公告栏的同学应该都知道,如果想让指定页面只能管理员评论,可以通过页面模板来实现,而文章是没有模板的,如果想让一篇文章只允许管理员评论,该怎么做呢?
下面是方法:
首先在你的comments.php找到

<?php if ('open' == $post->comment_status ): ?>

改成

<?php if ('open' == $post->comment_status ||current_user_can('level_10') ): ?>

这是第一步,现在看看在管理员的视角下,是不是关闭评论的文章也可以看到评论框咯?
但是还没有结素,因为你提交评论时会发现无法提交,因为"此项目评论已关闭"
接下来再改,如果用了ajax无刷新提交评论,需要修改comments-ajax.php,找到if ( !comments_open($comment_post_ID) ) {
把下面的

do_action('comment_closed', $comment_post_ID);
err(__('Sorry, comments are closed for this item.'));

注释掉,即:改成

//do_action('comment_closed', $comment_post_ID);
//err(__('Sorry, comments are closed for this item.'));

如果没有使用comments-ajax.php的,需要修改wordpress原生文件,找到根目录下的wp-comments-post.php

if ( !comments_open($comment_post_ID) ) {
	do_action('comment_closed', $comment_post_ID);
	wp_die( __('Sorry, comments are closed for this item.') );

改成

if ( !comments_open($comment_post_ID) ) {
	//do_action('comment_closed', $comment_post_ID);
	//wp_die( __('Sorry, comments are closed for this item.') );

现在就能成功提交评论了,而在游客眼里,依然是没有评论框的.
好开森,今天任务又完成了,匿~

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn