I put this together while working on a project as a way to manage totally custom reviews for products. Here’s an overview of the process (see here for a live demo):
- Creates a review custom post type
- Create custom meta data (custom fields) for the review CPT
- Create a form to accept reviews from users who have purchased the product
- Output the reviews below the product’s description (includes schema markup)
- Keep track of the number of reviews, their ratings and the average for each product
- Display each product’s associated reviews on the edit screen for quick admin access
Create a Review Custom Post Type
The class file I created takes care of this so you don’t need to worry about doing any of it yourself unless you want to modify the code in some way.
Create Custom Fields
Next, we need to create our custom data fields. I use Advanced Custom Fields Pro for this so, for this, tutorial, I’m going to assume that you are using it as well. Here’s a screenshot of how I set up the review fields (note that the keys must be the same in your setup unless you are able to change them in the code). If you’d like, here’s a gist to the import code so you don’t have to create the fields in ACF yourself.
The only thing worth mentioning about this ACF setup is that the edd_review_rating
field (radio button type) has the values 1, 2, 3, 4, and 5. These numbers are what a customer can rank a product (5 being the best).
Create a Form to Create the Reviews
To create the form, I’m using Gravity Forms and the Gravity Forms + Custom Post Types plugin (which makes it quicker to create custom posts with GF). For this tutorial, I also uploaded the import code that you can use to create the exact form I’m using (you’ll need to change confirmation text). Here’s a rundown of the fields I am using in the form along with their configurations.
- Review Headline (Title field – required) – Post status set to published, the logged-in user as the author with myself as the default, visible to everyone and saves to the Review post type.
- Download ID (Custom Field → Number – required) – Existing custom field name (edd_review_download_id), visible to admin only and
{embed_post:ID}
as the default value. - First Name (Custom Field → Single Line Text) – Existing custom field name (edd_review_first_name), visible to admin only and dynamic population enabled with a parameter of
user_fname
. - Last Name (Custom Field → Single Line Text) – Existing custom field name (edd_review_last_name), visible to admin only and dynamic population enabled with a parameter of
user_lname
. - Email (Custom Field → Email – required) – Existing custom field name (edd_review_email), visible to admin only and dynamic population enabled with a parameter of
user_email
. - Star Rating (Custom Field → Radio Buttons – required) – Existing custom field name (edd_review_rating), visible to everyone and choices 1-5.
- Leave a Review (Custom Field → Paragraph Text – required) – Existing custom field name (edd_review_review) and visible to everyone. I could have easily just used the regular editor for the review text but, for no specific reason, this is what I went with at the time.
The Class File
Now that we have the groundwork laid, we can move on to the code that will make everything work. Here it is and I’ll explain what it’s doing (and what you need to change) below.
Explanation and Changes
- The first few things this code is doing are things you mostly don’t need to mess with. It’s registering the review post type and storing a query of the reviews based on the currently shown download into a separate method. However, in the
__construct()
method, there are two calls to the gform_after_submission_1 action on lines 26 and 28. The “1” needs to be changed to whatever the form ID for your reviews is because this action only fires for the form with that ID. - The
rv_edd_download_reviews()
method outputs the reviews on the front-end and, if the user has purchased the download but not reviewed it yet, shows the form to leave a review. On line 227, you need to replace the shortcode with the Gravity Forms shortcode associated with your form. The most important things to change here will be the ID and Name attributes. - Lines 241-301 are responsible for adding an admin meta box to each download to show a list of all reviews for that download and an average. I also have it set to link to the review’s edit page if the logged in user is an administrator (otherwise, it just shows the rating and date).
- Lines 306-343 take care of dynamically populating the form fields we chose to have populated (the user’s first name, last name and email) when setting up the form. If you don’t want to collect this information, it’s not necessary but I included it in case you’d like to show the reviewer’s name, Gravatar, etc.
rv_update_user_meta_review_submit()
is a method that runs when the review form is submitted. It adds a meta key and value to the reviewer’s profile that is used to signify that the user has reviewed that specific product. This meta key-value is checked when displaying the review form earlier in the code.rv_update_user_meta_review_delete()
deletes that user meta if/when a review is sent to the trash to be deleted. This means that the user will be able to review that product again. You can get rid of this method if you don’t want this to happen.rv_remove_gf_review_submissions()
is a slick bit of code that deletes a form entry from your database after it is submitted. Since Gravity Forms doesn’t include an option for not saving specific form entries to the database, this is the best option. Since I’m creating posts with each entry, I don’t need or want the entry being stored as well and this code deletes it. Thanks to Thomas Griffin for the code.- Lastly, the final line of code instantiates (runs it) the class.
Finishing Up
Something you will want to keep in mind is the HTML markup within the front-end loop, as it’s specific to the Genesis Framework. You should change it to something that fits your theme.
That’s it! Have fun with it. 🙂
Leave a Reply