Advertisement
geminilabs

[site-reviews] Auto assign category to reviews with a title and content

Nov 7th, 2024 (edited)
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.94 KB | None | 0 0
  1. /**
  2.  * Modify the review values before they are saved
  3.  * Paste this in your active theme's functions.php file.
  4.  */
  5. add_filter('site-reviews/create/review-values', function (array $values): array {
  6.     // skip category assignment if the review title is empty
  7.     if (empty($values['title'])) {
  8.         return $values;
  9.     }
  10.     // skip category assignment if the review content is empty
  11.     if (empty($values['content'])) {
  12.         return $values;
  13.     }
  14.     // create the category if it doesn't already exist
  15.     $term = wp_create_term('full-review', glsr()->taxonomy);
  16.     if (is_wp_error($term)) {
  17.         // log the error if the category couldn't be created
  18.         glsr_log()->error($term->get_error_message());
  19.         return $values;
  20.     }
  21.     if (!empty($term['term_taxonomy_id'])) {
  22.         // assign the new review to the category
  23.         $values['assigned_terms'][] = $term['term_taxonomy_id'];
  24.     }
  25.     return $values;
  26. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement