Advertisement
geminilabs

[site-reviews] Dynamic custom form field labels

Mar 27th, 2022 (edited)
1,604
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.77 KB | None | 0 0
  1. /**
  2.  * @param array $fields
  3.  * @param \GeminiLabs\SiteReviews\Arguments $args
  4.  * @return array
  5.  */
  6. add_filter('site-reviews/review-form/fields', function ($fields, $args) {
  7.     $key = array_search('rating', array_column($fields, 'name'));
  8.     // make sure the rating field is in the form
  9.     if (-1 === $key) {
  10.         return $fields;
  11.     }
  12.     // make sure that this is a custom form (remove this if you are not using a custom Review Form!)
  13.     if (empty($fields[$key]['custom'])) {
  14.         return $fields;
  15.     }
  16.     // make sure that the form is assigning reviews
  17.     if (empty($args->assigned_posts)) {
  18.         return $fields;
  19.     }
  20.     $fields[$key]['label'] = sprintf('How do you rate %s', get_the_title((int) $args->assigned_posts));
  21.     return $fields;
  22. }, 20, 2);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement