Support

Account

Home Forums Backend Issues (wp-admin) Change value of read-only field Reply To: Change value of read-only field

  • You could go back to using the Date Picker and use JavaScript and JQuery to disable the fields.

    Here’s some code that I used in a recent project to do that, which demonstrates how to Disable various field types (Date Picker, Button Group/Radio Buttons, Select, Textarea, Repeater, WP Title)…

    
    /* START: Prevent editing of Support Ticket fields for specific roles */
    function cc_prevent_editing_support_ticket_fields_after_publish() {
      global $pagenow;
      if ( !is_admin() ) { return; } // only in wp-admin
      if ( $pagenow=='post-new.php' ) { return; } // not for 'new posts'
    
     // only for specific post type(s)
      $post_types = array('supporttickets');
      if ( !in_array( get_post_type(), $post_types ) ) { return; }
    
     // only for specific role(s)
      $user = wp_get_current_user();
      $user_role_id = $user->roles[0];
      $exempt_roles = array('administrator', 'owner', 'controller');
      if ( !in_array( $user_role_id, $exempt_roles ) ) {
    ?>
    <script type='text/javascript'>
      ( function ( $ ) {
        $( document ).ready( function () {
          $('#idOfDatePicker .acf-date-picker .hasDatepicker').prop('disabled','disabled'); // disable 'Date Picker' field
          $('#acf-field_59d9867971a81').prop('disabled','disabled'); // disable 'Select' field
          $('input[type="radio"][name="acf[field_59d94c7897682]"]').prop('disabled','disabled'); // disable 'Button Group' or 'Radio Buttons'
          $('#title').prop('disabled','disabled'); // disable WordPress Post Title
          $('#acf-field_59d968a0dc7a7').prop('disabled', true); // disable 'Textarea' field
          $('.acf-field-59daa7de17e69 .acf-actions').hover(function() {
            $(this).css('display','none');
          }); // removes delete button from 'Repeater Rows' on 'hover'
          $('.acf-field-59daa7de17e69 .acf-actions').css('display','none'); // removes delete button from 'Repeater Rows'
          $('.acf-field-59daa7ae17e68 a[data-event="add-row"]').remove(); // remove 'Add Row' button for 'Repeater' field
          $('.acf-field-59daa7ae17e68 a[data-event="remove-row"]').remove(); // remove 'Remove Row' button for 'Repeater' field
        });
      }( jQuery ) );
    </script>
    <?php
      }
    }
    add_action( 'edit_form_advanced', 'cc_prevent_editing_support_ticket_fields_after_publish' );
    /* END: Prevent editing of Support Ticket fields for specific roles */
    

    Hopefully that helps… you can target any HTML element in various ways and disable them. You can even add a specific ID or Class to your Date Picker fields in the Edit Fields screen for the field (next to ‘Wrapper Attributes’), and then target it that way (like I did at the top of JavaScript).

    ‘Disable’ is essentially the same as ‘Read Only’ for most purposes. However, when creating a *new* post, and you give a field a default value AND disable it, the value won’t Submit. So, if you are doing that… you will need to add more JavaScript that re-enables the form field upon click of the Submit button.

    At any rate, the above should get you going if you want to disable fields other than just the Basic fields.