I just realized Toolset cannot restrict access to posts based on taxonomies, so I have been putting conditional statements into my templates to accomplish this. The only thing left for me to tackle is comments. I have written some custom code that will check to see if a user is in a particular role and if a post has a particular term associated with it. Depending on the outcome of those if/then statements, the code will set a variable $pp_disable_comments to true. Where I'm getting stuck is what code to actually put in to prevent comments from displaying on that page.
Can you assist? I've indicated in capital letters towards the very bottom where I could use the help. You don't need to understand the whole code to be able to help, but I figured I'd put it here anyways.
add_action( 'the_post', 'pp_disable_comments' );
function pp_disable_comments($post_object) {
// only run if single page
if (is_single()) {
$pp_disable_comments = false;
$user = wp_get_current_user();
$roles = (array) $user->roles;
// skip if user is contributor/author/editor/administrator OR if the post is public
if ((!in_array('contributor', $roles) && !in_array('contributor', $roles) && !in_array('author', $roles) && !in_array('editor', $roles) && !in_array('xxadministrator', $roles)) || has_term('public', 'access-control')) {
// run if post is tier-0 only
if (has_term('tier-0', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-0', $roles) && !in_array('tier-1', $roles) && !in_array('tier-2', $roles)) {
$pp_disable_comments = true;
}
}
// run if post is tier-1 only
if (has_term('tier-1', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-1', $roles) && !in_array('tier-2')) {
$pp_disable_comments = true;
}
}
// run if post is tier-2 only
if (has_term('tier-2', 'access-control')) {
// check if user not in allowed roles
if (!in_array('tier-2', $roles)) {
$pp_disable_comments = true;
}
}
}
// disable comments if required
if ($pp_disable_comments == true) {
WHAT DO I PUT HERE? <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
}
}
}
The net result of this should be that comments disappear entirely from the post being loaded. I've scoured Google for articles but all solutions I find are for disabling them universally across the entire site or certain post types, so I would appreciate any tips from you. Thanks so much!!
Just the specific post that is being loaded on the single page, because it will be dependent on whether or not that post has a particular term associated with it. The code checks the user's role as well as the "Access Control" taxonomy and determines if the user is allowed to see that particular post, so it varies post to post.
That means if the user is allowed, then you want to grant permission to see the comment - right?
Can you please share one URL where you want to display the comment and one URL where you do not want to display the comment and accordingly the admin access details and frontend user access details that I should use. In addition to that please let me know where you added the code you shared.
*** Please make a FULL BACKUP of your database and website.***
I would also eventually need to request temporary access (WP-Admin and FTP) to your site. Preferably to a test site where the problem has been replicated if possible in order to be of better help and check if some configurations might need to be changed.
I have set the next reply to private which means only you and I have access to it.
That is not the right website. Do you still have that line added to your hosts file? The website you showed in your video would only be coming up if you didn't have the proper entry in your hosts file. Sometimes you do need to clear your browser cache after adding the line to your hosts file.
Yes - the line you asked to me to add to the host file is already added but still I could not able to login. Is there any different approach we can follow?
I have no idea why it isn't working. If the line is in your hosts file then the live site shouldn't be coming up, as it is.
Perhaps you don't need to login to provide assistance. I really just need advice on what PHP code I should use to stop the entire comments area from being rendered on a specific page. As the page loads, my script will check their role, but I don't know what code is used to disable the comments. See line 43 on the code included in the original support message above.
Thanks - would that close comments entirely or just for that one instantiation of the post? Also, I don't think that would actually remove the comments section from the page (it would only prevent you from adding new comments). I want it to be gone completely from the page.
Thanks - would that close comments entirely or just for that one instantiation of the post?
==>
Yes - thats correct.
Also, I don't think that would actually remove the comments section from the page (it would only prevent you from adding new comments). I want it to be gone completely from the page.
==>
Could you please just try that and let me know how it goes.