Somebody know if there is any filter or action to add field to the ajax_add_term modal on a taxonomy field ?
I found the render of the form here:
advanced-custom-fields-pro/includes/fields/class-acf-field-taxonomy.php line 856.
But i don’t find any filter.
I found a solution, but I’m sure we can do it cleaner.
In My theme:
## First I add somes helpers
function zk_acf_get_field_groups_by_tax($taxonomy){
$groups = acf_get_field_groups();
$filtered_groups = [];
foreach($groups as $key => $group):
foreach($group['location'] as $location):
foreach($location as $location_item){
if(
$location_item['param'] == 'taxonomy' &&
$location_item['operator'] == '==' &&
$location_item['value'] == $taxonomy
){
$filtered_groups[] = $group;
}
}
endforeach;
endforeach;
return $filtered_groups;
}
function zk_acf_get_fields_by_tax($taxonomy){
$groups = zk_acf_get_field_groups_by_tax($taxonomy);
$fields = [];
foreach($groups as $group){
$group_fields = acf_get_fields($group['key']);
if(is_array($group_fields)){
$fields = array_merge($fields, $group_fields);
}
}
return $fields;
}
## Then I copy class-acf-field-taxonomy.php in my theme and modify ajax_add_term function
function ajax_add_term() {
// vars
$args = wp_parse_args($_POST, array(
'nonce' => '',
'field_key' => '',
'term_name' => '',
'term_parent' => ''
));
// verify nonce
if( !acf_verify_ajax() ) {
die();
}
// load field
$field = acf_get_field( $args['field_key'] );
if( !$field ) {
die();
}
// vars
$taxonomy_obj = get_taxonomy($field['taxonomy']);
$taxonomy_label = $taxonomy_obj->labels->singular_name;
$custom_fields = zk_acf_get_fields_by_tax($field['taxonomy']);
// validate cap
// note: this situation should never occur due to condition of the add new button
if( !current_user_can( $taxonomy_obj->cap->manage_terms) ) {
wp_send_json_error(array(
'error' => sprintf( __('User unable to add new %s', 'acf'), $taxonomy_label )
));
}
// save?
if( $args['term_name'] ) {
// exists
if( term_exists($args['term_name'], $field['taxonomy'], $args['term_parent']) ) {
wp_send_json_error(array(
'error' => sprintf( __('%s already exists', 'acf'), $taxonomy_label )
));
}
// vars
$extra = array();
if( $args['term_parent'] ) {
$extra['parent'] = (int) $args['term_parent'];
}
// insert
$data = wp_insert_term( $args['term_name'], $field['taxonomy'], $extra );
// error
if( is_wp_error($data) ) {
wp_send_json_error(array(
'error' => $data->get_error_message()
));
}
// load term
$term = get_term($data['term_id']);
foreach($custom_fields as $custom_field){
$value = $_POST[$custom_field['name']];
if($value){
update_field($custom_field['name'], $value, $term);
}
}
// prepend ancenstors count to term name
$prefix = '';
$ancestors = get_ancestors( $term->term_id, $term->taxonomy );
if( !empty($ancestors) ) {
$prefix = str_repeat('- ', count($ancestors));
}
// success
wp_send_json_success(array(
'message' => sprintf( __('%s added', 'acf'), $taxonomy_label ),
'term_id' => $term->term_id,
'term_name' => $term->name,
'term_label' => $prefix . $term->name,
'term_parent' => $term->parent
));
}
?><form method="post"><?php
acf_render_field_wrap(array(
'label' => __('Name', 'acf'),
'name' => 'term_name',
'type' => 'text'
));
foreach($custom_fields as $field){
acf_render_field_wrap($field);
?>
<script>
acf.add_filter('prepare_for_ajax', function (data) {
data['<?php echo $field['name']; ?>'] = jQuery('#acf-<?php echo $field['key']; ?>').val();
console.log('data',data);
return data;
});
</script>
<?php
}
if( is_taxonomy_hierarchical( $field['taxonomy'] ) ) {
$choices = array();
$response = $this->get_ajax_query($args);
if( $response ) {
foreach( $response['results'] as $v ) {
$choices[ $v['id'] ] = $v['text'];
}
}
acf_render_field_wrap(array(
'label' => __('Parent', 'acf'),
'name' => 'term_parent',
'type' => 'select',
'allow_null' => 1,
'ui' => 0,
'choices' => $choices
));
}
?><p class="acf-submit">
<button class="acf-submit-button button button-primary" type="submit"><?php _e("Add", 'acf'); ?></button>
</p>
</form><?php
// die
die;
}
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!
We’re hard at work on ACF 6.1, and Beta 1 is now available 🚀
— Advanced Custom Fields (@wp_acf) March 16, 2023
This release includes custom post type and taxonomy registration, an improved experience when selecting field types, PHP 8.1 and 8.2 compatibility, and more!
Let’s take a look 🧵https://t.co/Y0WcAT11l4
© 2023 Advanced Custom Fields.
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.