Skip Navigation

[Resolved] Disabling Comments Programmatically on a Single Page

This support ticket is created 2 years, 5 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Kolkata (GMT+05:30)

This topic contains 21 replies, has 2 voices.

Last updated by aaronM-9 2 years, 4 months ago.

Assisted by: Minesh.

Author
Posts
#2203295

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!!

- Aaron

#2204423

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

I would like to know that do you want to disable comment for specific post or whole post type?

#2204955

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.

Thanks.

- Aaron

#2204969

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

#2205913

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Unfortunately the admin access details you shared is not working at this end. Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#2205995

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I already added that line to my host file but I'm not sure still its not working.

The access details you shared is not working yet.

#2206103
url-check.PNG

Does this screen match the following URL?

hidden link

#2206919

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Yes - I can see the same page. Please check the following screenshot: hidden link

Can you please send me working admin access details.

I have set the next reply to private which means only you and I have access to it.

#2206959

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

I'm not sure whats going on here. I'm still not able to login.

Please check the following video: hidden link

#2207027

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.

- Aaron

#2207839

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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?

#2207903

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.

- Aaron

#2207927

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to add the following code:

global $post;
// disable comments if required
        if ($pp_disable_comments == true) {
             $post->comment_status="closed";
        }

Does that helps?

#2207965

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.

- Aaron

#2208167

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

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.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.