Home › Forums › Backend Issues (wp-admin) › Change value of read-only field
Hi everyone,
I add to my function.php file some code to be able to choose wich wordpress role can see, or edit each field in the several custom posts I added.(acf_render_field_setting for adding the two new fields in the ACF management and the prepare_field filter to set the read-only param’)
Cause of several field type, like the datepicker, can’t be read-only, I first change the field type to ‘text’, but it’s clearly not userfriendly at all.
Do you have a solution (function or a filter) that’s allow me to change the displaying value but which not change the saved value ? (for the datepicker, value in database is still “yyyymmdd” but field are read-only and display “dd/mm/yyyy” ?
I can add “clone” for each field with enhanced message or others but I first want to know if there are a proper way to do …
Thanks,
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.
Hum sounds like a solution =D. And I just asking myself what to do with the add and remove button of repeater fields … Perfect !
In my case, field is disabled when “prepare_field” because I don’t know how much field I have and which permission is associated to (it’s set in the Edit Fields screen, with the settings fields I added) but I can add specific class during “prepare_field” and add the Javascript after with class selector.
I’ll try …
To confirm : it works perfectly ! Thanks !
I used prepare_field in all case, to set read only if it’s text or textarea field and to change the field’s class if I need JavaScript (and disabled field or/and actions with a class selector).
The topic ‘Change value of read-only field’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.