我正在尝试设置它,以便创建页面的人是唯一可以在该特定页面上发表评论的人。这将是一个登录的人。我在角色编辑器插件方面没有任何运气。
页面创建者仅留下评论
2 个回复
最合适的回答,由SO网友:karpstrucking 整理而成
也许有一种方法可以通过一个meta cap过滤器来实现这一点,但在我的脑海中,你可以使用comments_open
筛选以检查当前登录的用户是否是帖子的作者,并更改comments_open()
相应地发挥作用。
我在天井上,所以无法测试,但类似的方法应该可以:
add_filter( \'comments_open\', \'wpse158190_comments_open\', 10, 2 );
function wpse158190_comments_open( $open, $post_id ) {
global $current_user;
get_currentuserinfo();
$post = get_post( $post_id ); // \'global $post;\' may also work here
if ( $current_user->ID == $post->post_author ) return TRUE;
}
编辑:如评论中所述,除作者外,上述内容还禁止任何人查看评论。
查看comment_form()
核心功能,我看不到一个非常干净的方法来实现这一点。你可以在晚些时候comment_form_default_fields
筛选以删除字段:
add_filter( \'comment_form_default_fields\', \'wpse158195_comment_form_default_fields\', 99 );
function wpse158195_comment_form_default_fields( $fields ) {
global $current_user;
get_currentuserinfo();
$post = get_post( $post_id ); // \'global $post;\' may also work here
if ( $current_user->ID != $post->post_author ) unset $fields;
return $fields
}
然而,我怀疑这会留下一个无用的“留下回复”标题和“发表评论”按钮。您可以通过在页面上注入一些CSS来隐藏这些comment_form_before
措施:add_action( \'comment_form_before\', \'wpse158195_comment_form_before\' );
function 158195_comment_form_before() {
global $current_user;
get_currentuserinfo();
$post = get_post( $post_id ); // \'global $post;\' may also work here
if ( $current_user->ID != $post->post_author ) : ?>
<style type="text/css">
#respond.comment-respond { display: none; }
</style>
<?php endif;
}
SO网友:milo99
是的,它正在100%工作。这里是总插件,如果其他人需要它。。。
add_filter( \'comment_form_default_fields\', \'wpse158195_comment_form_default_fields\', 99 );
function wpse158195_comment_form_default_fields( $fields ) {
global $current_user;
get_currentuserinfo();
$post = get_post( $post_id ); // \'global $post;\' may also work here
if ( $current_user->ID != $post->post_author ) unset ($fields);
return $fields;
}
add_action( \'comment_form_before\', \'wpse158195_comment_form_before\' );
function wpse158195_comment_form_before() {
global $current_user;
get_currentuserinfo();
$post = get_post( $post_id ); // \'global $post;\' may also work here
if ( $current_user->ID != $post->post_author ) : ?>
<style type="text/css">
#respond.comment-respond { display: none; }
.comment-reply-link { display: none; }
</style>
<?php endif;
}
非常感谢karpstrucking结束