Autocomplete for taxonomy input boxes on a front end form

The question:

I have created a simple frontend form to allow users to submit posts – similar to the tutorial here – http://voodoopress.com/ . I also have a couple of custom taxonomies which i have as text input boxes on my form. This is all working but i was wondering if there is anyway i can have all the fields including Post Title and the taxonomies to auto suggest existing values?

thanks

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Ok not sure if this is the best method to use but it worked for me. I used a combition of jquery-ui-autocomplete – and the JSON API plugin

First download the required js files from the link above. I used jqueri-ui from the Google library
Install and activate JSON API plugin

I then used the following javascript to auto suggest when typing into the “title” input field on my custom post form

<script type="text/javascript">
$(function() {
        $('#title').autocomplete({
        source: function( request, response ) {
        $.ajax({
                url: "http://example.com/?json=1&include=title",
                dataType: "json",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 12,
                    name_startsWith: request.term
                },
            success: function( data ) {
            response( $.map( data.posts, function( item ) {
                return {
                            label: item.title,
                            value: item.title
                        }
                    }));
            }
            });
        },
        minLength: 2,
    });
});     
</script>

Next step is to get this working with custom taxonomies!

Method 2

I’ve spent a bit digging around on this and I think I’ve found your solution! It seems that get_terms()‘s name__like parameter is going to be the way to go. name__like‘s query is as follows (from line 1304 of /wp-includes/taxonomy.php): $wpdb->prepare( " AND t.name LIKE %s", $name__like . '%' );. It will take whatever you give it as the name__like parameter, and add an SQL wildcard, %, to it. This means that it will look for everything that STARTS with what you feed it.

That pretty much settles getting the data…unfortunately it doesn’t handle passing it to javascript. I’m not sure exactly how you’re handling that aspect of insertion, but I think I would pass out a an array just containing the names and let the discrimination of ‘do I create a new term or use the old one?’ be handled on submission. If you tell me which parts of this aren’t clear (I’m sure there will be some), I can try to give you some pseudo-code, but I don’t have time to write this whole thing up, unfortunately.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment