Advertisement
JoachimBrnd

Voxel improve required fields

Jan 6th, 2025
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.04 KB | Source Code | 0 0
  1. add_action('wp_head', 'custom_field_requirement_styles');
  2. function custom_field_requirement_styles() {
  3.     ?>
  4.     <style>
  5.         /* Style all required indicators first (excluding char counter) */
  6.         .ts-form-group .is-required:not(.ts-char-counter),
  7.         .ts-file-upload .is-required:not(.ts-char-counter),
  8.         span.is-required:not(.ts-char-counter) {
  9.             background-color: #8B0F0F1A !important;
  10.             color: #8B0F0F !important;
  11.             padding: 2px 10px !important;
  12.             display: inline-block !important;
  13.             border-radius: 4px !important;
  14.         }
  15.  
  16.         /* Style character counter */
  17.         .ts-char-counter {
  18.             background-color: #0666351A !important;
  19.             color: #066635 !important;
  20.             padding: 2px 10px !important;
  21.             display: inline-block !important;
  22.             border-radius: 4px !important;
  23.         }
  24.  
  25.         /* Hide only those marked as optional */
  26.         .is-required[data-optional="true"]:not(.ts-char-counter) {
  27.             visibility: hidden !important;
  28.             opacity: 0 !important;
  29.             pointer-events: none !important;
  30.         }
  31.     </style>
  32.     <?php
  33. }
  34.  
  35. add_action('wp_footer', 'custom_field_requirement_script');
  36. function custom_field_requirement_script() {
  37.     ?>
  38.     <script>
  39.     document.addEventListener('DOMContentLoaded', function() {
  40.         function handleRequirementLabels() {
  41.             const requirementSpans = document.querySelectorAll('.is-required:not(.ts-char-counter)');
  42.             requirementSpans.forEach(span => {
  43.                 if (span.textContent.includes('Optional')) {
  44.                     span.setAttribute('data-optional', 'true');
  45.                 } else {
  46.                     span.removeAttribute('data-optional');
  47.                 }
  48.             });
  49.         }
  50.  
  51.         // Initial call
  52.         handleRequirementLabels();
  53.  
  54.         // Observer for dynamic content
  55.         const observer = new MutationObserver(function(mutations) {
  56.             handleRequirementLabels();
  57.         });
  58.  
  59.         // Start observing
  60.         observer.observe(document.body, {
  61.             childList: true,
  62.             subtree: true
  63.         });
  64.     });
  65.     </script>
  66.     <?php
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement