Home › Forums › Add-ons › Repeater Field › Disable Repeater 'Add Row' Button
I’d like to modify my Repeater buttons/links based on the current User’s role, disabling them (or adding a class) so that only certain users with the proper capabilities can add more rows. After some searching here, I have what seems to be an almost-working solution, but I don’t think the buttons exist yet when my function runs (code shown below).
Is there a flag that I can listen for to know when all of the ACF fields and elements are finished loading? Thank you.
add_filter('acf/load_field', 'check_ACF_permissions');
function check_ACF_permissions($field) {
global $post;
if ($post->post_name == 'project-tracker') {
$field['readonly'] = 1;
?><script type='text/javascript'>jQuery('.acf-actions').data('event', '');</script><?php
$userRoles = ( km_get_user_role() );
foreach($userRoles as $r => $role) {
if ($userRoles[$r] == 'project-manager') {
$field['readonly'] = 0;
?><script type="text/javascript">jQuery('.acf-button').data('event', 'add-row');</script><?php
break;
}
}
}
return $field;
}
For reference, this thread was very similar but unfortunately no solution was presented:
Disable Update button until all fields have loaded
You would need to add you own custom JavaScript https://www.advancedcustomfields.com/resources/adding-custom-javascript-fields/. You could localize this with information about the current user and based on that you could remove the buttons. Unfortunately, without spending the time to actually build something that will do this I can’t really help you more than than.
Thanks for your reply; I got it working like this (using acf/load_field to first check permissions and set a global var):
add_action ( 'admin_footer', 'check_ACF_permissions_button' );
function check_ACF_permissions_button($post) {
if ($GLOBALS['projectManager'] === false) {
?><script type="text/javascript">
jQuery('a[data-event="add-row"]').remove();
jQuery('a[data-event="remove-row"]').remove();
</script><?php
}
}
Hello,
I resolved this problem with:
add_filter('acf/load_field/type=repeater', 'disable_repeater');
function disable_repeater($field) {
if($field['readonly'] || $field['disabled']){
?>
<script type='text/javascript'>
acf.addAction('load', function(){
jQuery('.acf-actions, .acf-row-handle').remove();
});</script>
<?php
}
return $field;
}
@vanian , @p-j-albuquerque Thanks for sharing!
I used acf/load_field/type=repeater
for new post & form generation (ie. not for edit)
Realizing that ALL repeaters are affected.
You must be logged in to reply to this topic.
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!
#DropzoneJS is now integrated into @ws_form allowing you to create no-code drag and drop file upload fields on your #WordPress forms. Fully integrated with @wp_acf. Fully responsive. Selfie feature! Learn more here: https://t.co/JUQmH2z315 pic.twitter.com/JhfO0LVQCm
— WS Form (@ws_form) January 8, 2021
© 2021 Advanced Custom Fields. Subscribe
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.