Integrate BuddyPress / WordPress Members in Customer Reviews Plugin

This tutorial can be skipped by using the new WordPress Review Plugin

This post is the next chapter in the WP Customer Reviews tutorial series, which will cover the basic integration of membership, whether WordPress or BuddyPress. So if you were looking for ways to require membership to post a review rating and/or integrate the name of members, simple follow the below steps or download the updated script for the plugin (although understanding this tutorial may yet be useful). This tutorial will cover:

Both features we will be integrating above will be done through the wp-customer-reviews.php file from the plugin or even the extended version you can download from the previous post.

Hiding Customer Review Form from Non-Members

If you intend to display the review form only to members, using the WordPress function is_user_logged_in() would be the way to go. Although there may be an easier way to do this, I chose to integrate this in the output section of the plugin incase you wanted to add some of your own features.

Open up your wp-customer-reviews.php file and find the function show_reviews_form(), which should be around line 934 in our extended file. Start by adding get_currentuserinfo(); on the first line of the function to retrieve the current user information, which we will also use to import the users member data. Then, at the end of the function, you should find the following:

return $out . $out2 . $out3 . $out4;

That line returns the whole review form - which we will modify to only display when the user is logged in. Simply replace the above line with the following:

if (is_user_logged_in()) {
	return $out . $out2 . $out3 . $out4;
}

The above will remove the form and button, but for those who want to trigger a call-for-action event would probably want to keep the button in place. To do this, you'd have to recombine the output in an else statement as follows:

if (is_user_logged_in()) {
	return $out . $out2 . $out3 . $out4;
} else {
	$register_user = '<p>In order to avoid duplicate reviews, please Login Your Account or <a href="/register/" target="_blank">Register</a>.</p>';
	return $button_html . '<div id="wpcr_respond_2">' . $req_js . $register_user . $out4;
}

You can replace $register_user with whatever you see fit, but the other variables remain in order to display the button and drop down from the wpcr's original function correctly.

Using and Displaying Member Information in Reviews

Like the comment form, WPCR requires an e-mail and username to post a review, but unfortunately seizes to recognize an already logged in member. With the first tutorial above, we were able to hide or replace the output if the user is logged out, so now we only have to consider the transferring the user data when logged in.

Here we have the choice to either grab the users data in WordPress or that from the BuddyPress profile. I noticed that the BP userdata is a bit more of a problem, so will be basing this example on BP, but you can easily make the same work for WordPress information or both. Here is a reference for the WordPress currentuserinfo function.

Integrating the BuddyPress User Data

Returning to the wp-customer-reviews.php file, go to the show_reviews_form() function, where we already placed the get_currentuserinfo() function and add the following to make it look like this:

function show_reviews_form() {
                get_currentuserinfo();
                global $post, $current_user,$bp, $user_login, $user_email, $ID, $xprofile_get_field_data;
		$userid = $bp->loggedin_user->userdata->ID; // BuddyPress ID
		$username = xprofile_get_field_data('First Name', $userid);	// BuddyPress First Name
		$username .= ' '.substr(xprofile_get_field_data('Last Name', $userid), 0, 1).'.'; // BP Last Names First Letter
		$useremail = $bp->loggedin_user->userdata->user_email; // BP User E-mail 
 
		$fields = '';
                $out = '';

Then, a bit further down, I replaced the original fields for name and e-mail with a hidden input field to avoid changing the requirement-verification process. To the that, find the below lines:

$fields .= '<tr><td><label for="' . $rand_prefixes[0] . '-fname" class="comment-field">Name: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[0] . '-fname" name="' . $rand_prefixes[0] . '-fname" value="' . $this->p->fname . '" /></td></tr>';

AND

$fields .= '<tr><td><label for="' . $rand_prefixes[0] . '-fname" class="comment-field">Name: ' . $req . '</label></td><td><input class="text-input" type="text" id="' . $rand_prefixes[0] . '-fname" name="' . $rand_prefixes[0] . '-fname" value="' . $this->p->fname . '" /></td></tr>';

And replace the above with the hidden fields containing the variables with:

$fields .= '<tr><td></td><td><input class="text-input" type="hidden" id="' . $rand_prefixes[0] . '-fname" name="' . $rand_prefixes[0] . '-fname" value="' . $username .'" /></td></tr>';
$fields .= '<tr><td></td><td><input class="text-input" type="hidden" id="' . $rand_prefixes[1] . '-femail" name="' . $rand_prefixes[1] . '-femail" value="' . $useremail .'" /></td></tr>';

Once the above changes are implemented, make sure that these fields are also activatet. I did this by removing the if statements that surround them, since those won't be needed. The above only helps bypass the verification process, so we will now do the final change to make sure it also gets submitted to your admin dashboard correctly, which is done in the add_review() function.

Submitting User Data with Review to Admin

To finalize, we will now just need to make sure the users data transfers correctly. Whether you include the users BuddyPress profile URL, Avatar or other info, the process should be the same, but for this example we will stick to the username and e-mail of the user submitting the review. In the add_review() function, find the following lines:

$date_time = date('Y-m-d H:i:s');
        $ip = $_SERVER['REMOTE_ADDR'];
 
        if (!isset($this->p->fname)) { $this->p->fname = ''; }
        if (!isset($this->p->femail)) { $this->p->femail = ''; }
        if (!isset($this->p->fwebsite)) { $this->p->fwebsite = ''; }

And import the username and e-mail into the corresponding variables:

$date_time = date('Y-m-d H:i:s');
        $ip = $_SERVER['REMOTE_ADDR'];
get_currentuserinfo();
global $post, $current_user,$bp, $user_login, $user_email, $ID, $xprofile_get_field_data;
		$userid = $bp->loggedin_user->userdata->ID;
		$username = xprofile_get_field_data('First Name', $userid);	
		$username .= ' '.substr(xprofile_get_field_data('Last Name', $userid), 0, 1).'.';	
		$useremail = $bp->loggedin_user->userdata->user_email;
        if (!isset($this->p->fname)) { $this->p->fname = $username; }
        if (!isset($this->p->femail)) { $this->p->femail = $useremail; }
        if (!isset($this->p->fwebsite)) { $this->p->fwebsite = ''; }

Latest Plugin File

The above scripts, including that of the previous post are now included in the business-reviews.zip file that members can download below. It's unlikely that any additions or further tutorials will be published soon in relations to the WPCR plugin, but if anyone does want to contribute tutorials, a different plugin or other solutions to this is then free to do so.

Download “Multi Business Member Reviews” business-reviews.zip – Downloaded 731 times – 26 KB

I'm a developer at Maximus Business.
  • Adrian
    hi maximus download link is no found on this page.

    Said on Apr 6, 2015 by Adrian - Reply

Leave a Reply

Please feel free to give us your feedback and comment below. Please keep in mind that comments are moderated. Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>