Home › Forums › Feature Requests › create new taxonomy term
hi,
it would be really great, if new taxonomy terms could be added directly in the input field (just writing it there and pressing enter). this would be a great improvement, if you have to add a lot of new terms.
thanks
You can. There is a setting on taxonomy field, “Create Terms”. If this is set to yes then you and add new terms directly in the ACF field. When editing and you hover over the field there will be a “+” icon. Click this to add a new term.
yes but you still have click this + icon each time and create a single tag in the popup window. selecr2 has the capability to create tags direct in the input field regularly:https://select2.org/tagging#tagging-with-multi-value-select-boxes
if i enter a term that does not exist and press enter, the term is created directly
Agreed. This would be a massive improvement. Has anyone figured out how to do this?
The current implementation is extremely cumbersome. Particularly if we’re having users add their own custom tags, people love adding many tags. Having to do this multi-step process as it currently is is tiring and results in most of our users skipping this field entirely.
Hi,
Put this in your child theme functions.php file :
function jl_insert_term_if_not_exists($term, $taxonomy) {
if (empty($term) || is_numeric($term)) {
return $term;
}
if ($result = term_exists($term, $taxonomy)) {
return $result['term_id'];
}
$result = wp_insert_term($term, $taxonomy);
if (!is_wp_error($result)) {
return $result['term_id'];
}
}
function jl_acf_update_value( $value, $post_id, $field ) {
if ($field['type'] == 'taxonomy'
&& in_array($field['field_type'], array('select', 'multi_select'))
&& strpos($field['wrapper']['class'], 'acf-select-tags') !== false)
{
if (!is_array($value)) {
if ($term_id = jl_insert_term_if_not_exists($value, $field['taxonomy'])) {
$value = $term_id;
}
} else {
foreach ($value as &$item_value) {
if ($term_id = jl_insert_term_if_not_exists($item_value, $field['taxonomy'])) {
$item_value = $term_id;
}
}
}
}
return $value;
}
add_filter('acf/update_value', 'jl_acf_update_value', 5, 3);
function jl_acf_input_admin_footer() {
?>
<script type="text/javascript">
(function($) {
acf.add_filter('select2_args', function( args, $select, settings, $field ){
if ($field.hasClass('acf-select-tags')) {
args.tags = true
}
return args;
});
})(jQuery);
</script>
<?php
}
add_action('acf/input/admin_footer', 'jl_acf_input_admin_footer');
If you want to enable the select2 “tags” function (add with enter key) go in your Taxonomy field (select or multi-select type) options, in wrapper / class, set “acf-select-tags”.
Hope this helps.
PS : Then, you can disable the “can create terms” option if you want to get rid of the “plus” button.
It doesnt work with dates and numeric values… If Insert date or numeric value then it inserts term ID
The topic ‘create new taxonomy term’ 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.