Easy Digital Downloads – Restricting Comments Viewing & Submission

Welcome to the second part of the series on Easy Digital Downloads and comments customization. In this part we’ll go over restricting comments ( viewing and submission ) based on whether the visitor is a customer or not.

Custom “has user purchased” Function

Easy Digital Downloads comes with a function called edd_has_user_purchased() but we can’t just call that function in it’s pure form because:

  • If EDD is not enabled it will trigger a PHP error
  • It does not have a default for the user id
  • It does not have a default for post id ( download id )

So we would need to do extra checks every time we use that function. Instead we’ll create a custom function that combines everything, so we can simply call it like this:

And here’s what that new function looks like:

Restrict Comment Submit

Let’s say you want to use the comments to offer support for the product/download, but you only want to allow the users who have purchased the download to comment.

What we need to do is tell WordPress that the comments are closed if a non-customer is viewing the page. And we can do that by hooking into the comments_open filter.

In most themes when comments are closed a message saying “comments are closed” will show up, which is not entirely correct in this case. So we need to show a special message saying that only customers can comment.

We will be altering the comments.php file of the theme. Near the end of the file you should see this code:

We’ll simply do a check to see if we are on a single download and if the user did not purchase. If that is the case we show a custom message, if not we show the “comment closed” message.

Restrict Comments View

Other than restricting the ability to submit comments you might also want to restrict the ability to view existing comments. And there are a few different approaches to that:

  1. Show the comments listing but do not show the comments content
  2. Show the comments listing but do not show comment replies
  3. Do not show the comments listing at all

Do Not Show Comments Content

The content of all comments would be replaced with a notification and a link to log in as in the screenshot above. The code to achieve that is:

Do Not Show Comment Replies

A bit less restrictive approach is to show the original comments but hide replies. We just need to alter the if statement a bit to check if the comment has a parent ( if it does then it’s a reply ).

Do Not Show Comments Listing

The most restrictive approach would be to not show the comments listing at all. Unlike the other approaches, for this one we won’t be hooking into any WordPress filters, we will change the comments.php of the theme.

In every theme there’s a big chunk of code wrapped by an if statement with the function have_comments(), like this:

So what we want to do is wrap all that in another if statement where we check if the user has purchased the download.

Final Words

That’s all for part 2 of this series. If you liked this article/tutorial please consider sharing it on Twitter or Facebook, it would be of great help.

Previous Post

Common Reasons For Theme/Plugin Conflicts in WordPress

Next Post

Easy Digital Downloads – Enabling Comments & Adding “Purchased” and “Author” Badges

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.