Advertisement
geminilabs

Untitled

Mar 23rd, 2022 (edited)
1,395
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.04 KB | None | 0 0
  1. /**
  2.  * This will prefill the Rating field and an Assigned Posts custom field value using URL parameters
  3.  *
  4.  * Example: https://website.com?assigned_posts=123&rating=5
  5.  *
  6.  * @param array $fields
  7.  * @param \GeminiLabs\SiteReviews\Arguments $args
  8.  * @return array
  9.  */
  10. add_filter('site-reviews/review-form/fields', function ($fields, $args) {
  11.     // Assigned Posts
  12.     $assignedPosts = filter_input(INPUT_GET, 'assigned_posts', FILTER_VALIDATE_INT);
  13.     if (false !== $assignedPosts) {
  14.         $key = array_search('assigned_posts', array_column($fields, 'name'));
  15.         if (-1 !== $key) {
  16.             $fields[$key]['value'] = $assignedPosts;
  17.         }
  18.     }
  19.     // Rating
  20.     $rating = filter_input(INPUT_GET, 'rating', FILTER_VALIDATE_INT, [
  21.         'options' => [
  22.             'min_range' => 0,
  23.             'max_range' => 5,
  24.         ]
  25.     ]);
  26.     if (false !== $rating) {
  27.         $key = array_search('rating', array_column($fields, 'name'));
  28.         if (-1 !== $key) {
  29.             $fields[$key]['value'] = $rating;
  30.         }
  31.     }
  32.     return $fields;
  33. }, 30, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement