Skip Navigation

[Resolved] Disabling Comments Programmatically on a Single Page

This support ticket is created 2 years, 6 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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

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, 5 months ago.

Assisted by: Minesh.

Author
Posts
#2208181

That did not work. Is 'the_post' the right action to use (line 2 in my code)?

- Aaron

#2208601

Minesh
Supporter

Languages: English (English )

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

Well - honestly I would like to help you but until and unless I check your setup it would be really time consuming as it will be like I'm sharing the solution without knowing anything.

Do you have any other setup where I can apply the code - is it possible for you to setup another test site on another test install where I can play it and get back to you with the solution?

#2208763

Okay, I have an idea. Go to sandbox.peakprosperity.com instead. It's an older clone of the development site but I've copied over the code snippet and you should be able to trouble shoot there. I made sure the password is the same as the one previously sent (ending in 4YZm). You shouldn't need anything in your hosts file for this. Let me know if that works. Thanks.

- Aaron

#2208773

Minesh
Supporter

Languages: English (English )

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

It is asking me for the authorization access details when I try to load the site.

Can you please share authorization access details so that I can access the sandbox site.

#2209951

Apologies for not seeing this response earlier. Here you go:

Username = peak
Password = peak!

We will be re-cloning the site in the next 24 hours so if that username stops working, try "sneak" instead.

- Aaron

#2209985

Minesh
Supporter

Languages: English (English )

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

We will use the WordPress hook "template_redirect" and I've adjusted the code.

I've adjusted the code as given under within the code snippet:

add_action( 'template_redirect', '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;
		
		
		
// NEED TO CAHNGE ADMINISTRATOR BACK AFTER DONE TESTING
		// 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) {
			
			global $post;
			$post->comment_status="closed";
			
			
			
		}
		
	}
	
}

When I visit the following page I can see the comment section is not available: hidden link

That means comment section is gone that is expected result.

Can you please confirm it works at your end as well.

#2210007

I can confirm it appears to be working now. It is not showing comments when unauthenticated and it is showing comments when authenticated. Thanks for the help!

- Aaron

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