Support

Account

Home Forums ACF PRO ACF taxonomy input text

Solving

ACF taxonomy input text

  • I am developing a Field Group with multiple fields for post_tags output with ACF PRO. The final user find very difficult to make the tags first and then choose them from a checkbox or a select.

    Is it possible to take the values from a Text Input in a Taxonomy field and convert them directly in post tags when save the post?

    Thanks
    Best regards

  • Hi @solserpiente,

    The conversion of text inputs to pst tags would only work for single value custom fields in which case you would need the following JQuery function for the implementation(it should go to your functions.php file):

    <?php 
     
    function convert_custom_fields_to_tags(){ ?>
     
      <script type="text/javascript">
        jQuery(document).ready(function($){          
     
            // Create list of custom fields to add as tags on save
            // (e.g. var custom_field_names = ['my_custom_field', 'my_other_custom_field', 'yet_another_custom_field'];)
            var custom_field_names = [];
     
            $('form#post').submit(function(){
                if(custom_field_names.length > 0) {
                    var custom_field_values = [];
                    $('#postcustom tr[id^="meta-"]').each(function(){
                        var meta_id = $(this).attr('id').substring($(this).attr('id').indexOf('-')).replace('-','');
                        if ($.inArray($(':text[id="meta[' + meta_id + '][key]"]').val(), custom_field_names) !== -1) {
                            custom_field_values.push($('textarea[id="meta[' + meta_id + '][value]"]').val().toLowerCase());
                        }
                    });
                    var tags = custom_field_values.join(',');
                    $('#new-tag-post_tag').val(tags);
                }
            });
     
        });
      </script>
    <?php }
    add_action('admin_footer', 'convert_custom_fields_to_tags');
     
    ?>

    You would then need to add the custom field names to the custom_field_names array as follows:

    var custom_field_names = ['my_custom_field', 'my_other_custom_field'];

  • This works great..!
    Is it also possible to add a new tag when typing and pressing enter?

  • Hi @geppie030,

    Thanks a lot for contacting us.

    For such an implementation you would have to implement the Jquery function on the keydown event.

    $("#fieldId").keydown(function (e) {
      if (e.keyCode == 13) {
       //write the implementation here.
      }
    });

    I hope this helps.

  • Hey James,

    It looks like the enter keydown event is not working on the select2.js

    I made this to call the wp_insert_term() and when changing the keycode its working but when using keycode 13 its not working:

    jQuery(".acf-taxonomy-field").keydown(function (e) {     
    	    var code = e.keyCode || e.which;     
    	if(code == 13) { 
    	 
    	var post_id = jQuery(".select2-search-field input").val();
    	jQuery.ajax({
               type:"POST",
               url: ajaxurl,
               data: {
                   action: "formulier_ajax_request",
                   id: post_id
                     },
               success:function(data) {
    	            alert('Toegevoegd');
                     },
               error: function(errorThrown){
               alert(errorThrown);
                    } 
             }); 
             }
    });	
    
  • Hi @geppie030,

    Thanks for the follow up.

    That is quite odd. Could you kindly try to do a console for the code value before the if control structure to see if the value is correctly returned?

    Let me know how it goes.

Viewing 6 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic.