I think you can trap JS hooks to add dynamic content to row fields once a new row has been added.
I think the clone field is for you.
You can create field group then clone it elsewhere.
https://www.advancedcustomfields.com/resources/clone/
Oups last solution failed in some cases…
But I think that this one is better and simpler (but not fully tested) ..
`// Get repeater field (replace my-repeater-field-key’ with a field key)
repeater = acf.getField(‘my-repeater-field-key’);
// Add a new row
$row = repeater.add();
// Get subfields
let sub_fields=acf.getFields({ parent: $row});
// show sub fields
console.log(sub_fields);
// loop around sub fields and populate them.
@pzh20 , @hube2 I’ve just played around repeaters in JS.
Here the basic code used to add a row and to retrieve all the sub fields.
// Get repeater field (replace my-repeater-field-key' with a field key)
repeater = acf.getField('my-repeater-field-key');
// Add a new row
$row = repeater.add();
// Get subfields
let sub_fields=[];
$row.children('td.acf-field').each((i,$el)=>{
sub_fields[i] = acf.getField($el);
})
// show sub fields
console.log(sub_fields);
// loop around sub fields and populate them.
Not so tricky with ACF JS API…
@pzh20, yes it is the repeater field key then you can use the add method to create a new row.
Hello
Using the JS API , this is really simple
my_repeater = acf.getField('<my key>');
my_repeater.add();
RogerBSulli, I think the best is to open first a new thread.
Regarding your problem, it coulb be easily solved with few JS code.
Thanks for the feedback.
Yes by using both PHP and JS API I was able to find a solution !
Sure.
Add it into your plugin main PHP file.
On PHP there’s no action to trigger 🙂 but a filter. Do not encapsulate the filter hook in any action but use it as is in your functions.php.
You did not use the right hook to enqueue your script in the admin part.
The best way to do this when you are using ACF is to use the dedicated action.
Heres an example to enqueue a my.js file from <MY PLUGIN>/js
add_action( 'acf/input/admin_enqueue_scripts', function () {
wp_register_script( 'my-js', esc_url( plugins_url( '/js/my.js', __FILE__ ) ), false, false, true );
wp_enqueue_script( 'my-js' );
} );
Here is a more complete example .
UC : you want to change the content of some fields based on the selection of a combo box.
Here the JS part :
jQuery(document).ready(($) => {
acf.addFilter('select2_ajax_data',(data, args, $input, field, instance) => {
// Get the combo field instance
let your_combo = acf.getFields({
name: 'your_combo_field_name'
})[0];
if (your_combo !== undefined && (data.field_key === field.get('key')) {
data.your_value = your_combo.val(); // Here is the magic ;)
}
}
});
})
And on PHP side, for all the fields depending on the combo selected value
foreach (['field1','field2','field3',] as $name) {
add_filter( 'acf/fields/post_object/query/name=' . $name, function ( $args, $field, $post_id ) {
// get the value from AJAX or from the field value
$list = $_POST['your_value'] ) ?? get_field( 'your_combo_field_name', $post_id );
}
// change the query according to your model
if ( null !== $list ) {
// $args['tax_query']
// $args['meta_query']
}
return $args;
}, 1, 3 );
}
Hope this helps. Do not hesitate to comment if you find any problem in the code (wrote online from a live example)
This can be done by :
– 1st : pusching the selected data to the ajax request. For this you should use
select2_ajax_data javascript filter and add a new element in the ajaxData array using the ACF JS API.
Let say ajaxData.myparameter = getField(yourselectbox)
– 2nd : Once this is ok, use the acf/field/post_object/query on php side.
This filter is used to retrieve information and add them to the query.
For this get $_POST[myparameter] ?? get_field(yourselectbox).
$_POST : used to get ajax value from JS.
get_field() : used when you update the page to used the current saved value for yourselectbox.
Then you update the query to get the value for the other fields.
The walkthrough to follow.
On javascript you should
– get the user ID (there are functions to do that)
– add it to the ACF ajax query.
For the other fields depending on user ID value :
On PHP you should use query filters (there are some for taxs, posts, relations)
– get the $_POST[ID] (added by JS before)
– use it.
When you load the page (ie for an update) , $_POST[ID] does not exist but you can use get_field if you need to use your query filters with user ID
ACF JS API can be used to manage fields to change ajax query, to interact with action events and filters, manage dynamic relation between fields…
The main advantage is that you can manage fields and group using the same ACF logics as with PHP
Another solution is to use ACF Javascript API and manage onchange event on your fields then add/remove the disable class when required.
1st of all, sorry for the late reply.
Yes sure I’ve done some code that allows to manage group of fields in order to add the possibility to make some value inclusion, exclusion.
By adding a specific class to some fields, you can :
– group fields
– insert value from a group
– exclude value from a group
– Mix all this.
It is a JS lib (but with some jQuery code, not 100% migrated to vanilla js ).
It is running everyday without any problem (in my site context) without enhancements since more than one year.
You can find it here : https://github.com/chdenat/acf-xtend
Feel free to contact me for further explanation.
Hello,
Just add it in your render block function in pure HTML , nothing to do with ACF
Christian
Did this in my tiny lib where I can dynamicaly populate ACF select list with the content of other ACF selected items.
Feel free to check the code at https://github.com/chdenat/acf-xtend !
The idea is to get the select associated to any select field and manage its content accordingly.
Hmmm… Got exactly the same issue here. I have an older Field group attached to a post type that appears when I edit my posts, but that are not in le ACF list anymore.
Thanks for the quick answer.
It was causing me an issue because I’m trapping repeater row creation events to maintain a set of data I need to the ‘field sync tool’ I’m developping.
This was based on rows number in id… but found another way to do what I want !
Partially done.
All in Javascript.
– able to dynamically get selections and put them in a global array.
– able to filter the selection choices with acf JS filter.
Need to put all these things together… and exclude already selected items.
stay tuned
Exactly what I’m looking for but for a soccer team 😉 . I’m investigating on this and check some solutions.
It is not possible on php side because there is no hook when you add/remove an item in one field.
Seems to be possible using JS API… but I need to pass items list (available in PHP)
What I’ll try :
managing add/remove hook using JS API
launch an ajax request that manage the selected list (as global) and add/remove items depending on the selection.
I need to filter the available items through javascript by reading through ajax the PHP list …
Should do the trick but it seems hi level dev…
Forgot It.. Sorry . I’ve found that another plugin I use have some improvements and the method I use return an array of post instead of a unic value….
Using update field does not work for date (it works for string).
So the solution I’ve put in place is :
$this->birthdate = date( "Ymd", $this->birthdate );
update_post_meta ($this->ID, 'birthdate', $this->birthdate);
update_post_meta ($this->ID, '_birthdate', 'field_53e11dd3841f6'); // CONSTANT KEY
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.