Home › Forums › Feature Requests › create new taxonomy term › Reply To: create new taxonomy term
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.
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.