For anyone else trying to do this, you can get the field key with:
$field.data('key')
Now you can do something like this:
if ($field.data('key') == 'field_609a8f6d92e06') {
args.palettes = [ "#F00", "#0F0", "#00F" ];
} else if ($field.data('key') == 'field_60a64e4b2f452') {
args.palettes = [ "#FF0", "#0FF", "#F0F" ];
}
I was getting 500 errors:
Uncaught ArgumentCountError: Too few arguments to function my_acf_upload_prefilter(), 1 passed in xxx/wp-includes/class-wp-hook.php on line xxx and exactly 3 expected
because this line:
add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter');
needs the number of parameters:
add_filter('acf/upload_prefilter/name=my_acf_upload_fieldname', 'my_acf_upload_prefilter', 10, 3);
Sorry to dig up an old post, but I can’t seem to get this to work with sub fields (through a repeater).
I know John suggested the field key on another thread but I’ve had no luck.
Just make sure that my_acf_upload_fieldname is your sub_field name (this should be a File field), not the repeater field name.
It turns out you can call acf_update_field with almost the same parameters as acf_add_local_field, except I just had to pass the parent as a post_id. Here’s the (simplified) code that I used:
function get_acf_group_id($group_name){
global $wpdb;
return $wpdb->get_var("
SELECT ID
FROM $wpdb->posts
WHERE post_type='acf-field-group' AND post_excerpt='$group_name';
");
}
function my_field_registration( $value, $post_id, $field ) {
if ( $field['name'] == 'my_triggering_field' ) {
if ( $some_condition ) {
$args = array(
'key' => 'field_' . uniqid();,
'label' => 'Field label',
'name' => 'my_new_field_name',
'type' => 'number',
'parent' => get_acf_group_id('my-field-group-name') // hyphenated!
);
acf_update_field($args);
}
}
return $value;
}
add_filter('acf/update_value', 'my_field_registration', 10, 3);
This creates a field programmatically in the database which is editable in the admin.
Thanks for your suggestion John, but that would require me to know all possible field names in advance (so I could hide/show the correct one conditionally). There are infinite possibilities in my case.
What about using acf_update_field to write the field to the database? From includes/api-field.php:
/*
* acf_update_field
*
* This function will update a field into the DB.
* The returned field will always contain an ID
*
* @type function
* @date 1/10/13
* @since 5.0.0
*
* @param $field (array)
* @return $field (array)
*/
function acf_update_field( $field = false, $specific = false )
I could construct a field object and call this function perhaps?
I should clarify that I want the registered field to appear in the admin UI.
Should I be creating the field as an acf-field custom post type in the database?
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.