I am trying to set it so that the person who created a Page is the ONLY one who can comment on that particular page. This will be a logged in person. I haven't had any luck with Role Editor Plugins.
2 Answers
There's probably a way to do this via one of the meta cap filters, but off the top of my head you could use the comments_open
filter to check if the currently logged in user is the author of the post and change the output of the comments_open()
function accordingly.
I'm on the patio, so can't test, but something like this should work:
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;
}
Edit:
As noted in comments, the above will also disallow anyone but the author from seeing comments.
Looking through the comment_form()
function in core, I don't see a very clean way to accomplish this. You could run a late hook of the comment_form_default_fields
filter in order to remove the 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
}
However, I suspect this will leave a useless "Leave a reply" heading and "Post Comment" button. You could hide these by injecting some CSS on the page via the comment_form_before
action:
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;
}
-
-
If you want it as a plugin, just put it in a new file in the plugins folder, with the appropriate header information included. Commented Aug 17, 2014 at 20:30
-
yeah maybe I should have been more specific. I need the author of the page to be the ONLY one to be able to leave a comment on their own page but I need EVERYONE to be able to view this comment but without the ability to reply or leave a comment themselves. Is there a read only filter or something like that?– milo99Commented Aug 17, 2014 at 21:28
-
-
sorry, I forgot to wrap the CSS in the same conditional logic; updated. Commented Aug 18, 2014 at 18:22
Yes it is working 100%. Here is the total plugin if anyone else needs it...
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;
}
Thank You so much karpstrucking
-
1When you add code to the editor, highlight it, and then click on the
{}
sign just above the text box. This will add your code in a code block as in my edit Commented Aug 18, 2014 at 18:53 -