Advertisement
geminilabs

Restrict maximum title length

Oct 23rd, 2021 (edited)
1,212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.81 KB | None | 0 0
  1. /**
  2.  * Restrict the title to 80 characters (client side)
  3.  * @param array $field
  4.  * @return array
  5.  */
  6. add_filter('site-reviews/field/text', function ($field) {
  7.     if ('title' === $field['path']) {
  8.         $field['maxlength'] = 80;
  9.     }
  10.     return $field;
  11. });
  12.  
  13. /**
  14.  * Restrict the title to 80 characters (server side)
  15.  * @param array $rules
  16.  * @return array
  17.  */
  18. add_filter('site-reviews/validation/rules', function ($rules) {
  19.     if (array_key_exists('title', $rules)) {
  20.         $rule = 'max:80';
  21.         // change any existing max: rule
  22.         $rules['title'] = preg_replace('/(max:\d+)/', $rule, $rules['title'], -1, $result);
  23.         // if no max: rule was found, then add it
  24.         if (0 === $result) {
  25.             $parts = explode('|', $rules['title']);
  26.             $parts[] = $rule;
  27.             $rules['title'] = implode('|', $parts);
  28.         }      
  29.     }
  30.     return $rules;
  31. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement