Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- //
- // ADDING CUSTOM FIELDS TO THE REVIEW FORM IS NOT ACTIVELY SUPPORTED!
- // However, if you would like to try and do this on your own, this should help get you started.
- // This will only work with Site Reviews 6/7
- //
- // Alternatively, please see: https://niftyplugins.com/plugins/site-reviews-forms/
- //
- /**
- * Modify the review form fields here
- * @param array $config
- * @return array
- */
- add_filter('site-reviews/config/forms/review-form', function ($config) {
- $config['opinion'] = [
- 'label' => __('Provide an opinion', 'your_theme_domain'),
- 'required' => true,
- 'type' => 'textarea',
- ];
- $config['service_rating'] = [
- 'label' => __('Select a rating for the service', 'your_theme_domain'),
- 'required' => true,
- 'type' => 'rating',
- ];
- return $config;
- });
- /**
- * Perform custom field validation here
- * @param bool $isValid
- * @param array $requestData
- * @return bool|string
- */
- add_filter('site-reviews/validate/custom', function ($isValid, $requestData) {
- // return true on success
- // or return false on failure
- // or return a custom error message string on failure
- return $isValid;
- }, 10, 2);
- /**
- * Triggered immediately after a review is saved
- * If you are uploading files, this is where you would save them to the database and attach them to the review
- * @param \GeminiLabs\SiteReviews\Review $review
- * @param \GeminiLabs\SiteReviews\Commands\CreateReview $command
- * @return void
- */
- add_action('site-reviews/review/created', function ($review, $command) {
- // You may access the global $_POST and $_FILES here.
- }, 10, 2);
- /**
- * Set the default values of the custom fields here
- * @param \GeminiLabs\SiteReviews\Review $review
- * @return \GeminiLabs\SiteReviews\Review
- */
- add_action('site-reviews/review/build/before', function ($review) {
- if (!isset($review->custom->opinion)) {
- $review->custom->set('opinion', '');
- }
- if (!isset($review->custom->service_rating)) {
- $review->custom->set('service_rating', 0);
- }
- });
- /**
- * Render the custom review fields here
- * In order to display the rendered custom fields, you will need to use a custom "review.php" template
- * as shown in the plugin FAQ ("How do I change the order of the review fields?")
- * and you will need to add your custom template tags to it, for example: {{ name_of_your_custom_field }}
- * @param array $templateTags
- * @param \GeminiLabs\SiteReviews\Review $review
- * @return array
- */
- add_filter('site-reviews/review/build/after', function ($templateTags, $review) {
- foreach ($review->custom as $key => $value) {
- if ('service_rating' === $key) {
- $value = glsr_star_rating($value); // render the stars from the rating value
- }
- if (is_array($value)) {
- $list = array_reduce($value, function ($result, $item) {
- return "{$result}<li>{$item}</li>";
- });
- $value = "<ul>{$list}</ul>";
- }
- $templateTags[$key] = "<div class=\"glsr-custom-{$key}\">{$value}</div>";
- }
- return $templateTags;
- }, 10, 2);
- /**
- * Enable the Custom Fields metabox to display the values of the submitted custom fields
- * @return void
- */
- add_action( 'admin_init', function() {
- add_post_type_support('site-review', 'custom-fields');
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement