3

I'm using this autocomplete script https://github.com/agarzola/jQueryAutocompletePlugin within my wordpress theme. I have a form in a page where the user could specify some tags. Actually, this is the code I'm using and it's working good.

    <script>
        jQuery(function() {
            var data = '<?php global $wpdb; $search_tags = $wpdb->
                   get_results("SELECT name FROM $wpdb->terms"); 
                      foreach ($search_tags as $mytag){ echo $mytag->name. " "; } ?>'.split(" ");
         $("#tags").autocomplete(data,{multiple: true});
            });
</script>

Now, the problem is, this kind of query puts all the tags inside an array when I load the page. This could work if you have 100 tags, but we have a lot more. The script has also an autocomplete from remote script option as shown below:

<script type="text/javascript">
jQuery().ready(function() {
    $("#tags").autocomplete("<?php bloginfo('template_url'); ?>/get-tags.php", 
    {
        width: 260,
        matchContains: true,
        selectFirst: false,
        multiple: true
    });
});
</script>

This must activate autocomplete as the user types. This is the code for get-tags.php (I put the file in my theme root)

<?php


include_once(‘../../../wp-config.php’);
include_once(‘../../../wp-load.php’);
include_once(‘../../../wp-includes/wp-db.php’);


global $wpdb; 
    $search_tags = $wpdb->get_results("SELECT name FROM $wpdb->terms"); 
        foreach ($search_tags as $mytag)
            { echo $mytag->name. " "; }


?>

I have tried everything and searched online for hours but I didn't find a solution on how to call the get-tags.php, it gives me an internal 500 error and the autocomplete doesn't work anymore. thanks

5
  • 1
    You could turn this into an ajax request and have your ajax callback do the term fetching via admin-ajax.php, you'd then not need the extra file in your theme(with all those nasty looking includes).
    – t31os
    Jan 9, 2011 at 23:26
  • @t31os How would you trigger the request for the autocomplete?
    – kaiser
    Dec 23, 2011 at 15:06
  • You ask me now after 11 months... lol... offhand, no idea, but i don't typically say something unless i think it's possible. Seeing as the asker got an answer he was happy with, i think i'll leave it at that... :) Valid point though.. (i'm terrible for drive-by comments)..
    – t31os
    Dec 23, 2011 at 15:32
  • Yep, the answer worked for me. As I remember I had a little issue with the separator, but I found a workaround.
    – Andycap
    Dec 24, 2011 at 15:11
  • @t310s Guys, I'm asking because of this Q. Yes, I'm not only here to answer Qs ;)
    – kaiser
    Dec 27, 2011 at 13:04

1 Answer 1

4

don't include WP like that. use $_GET instead:

...
$("#tags").autocomplete("<?php echo add_query_arg('get_my', 'terms', home_url()); ?>", 
...

theme's functions.php:

add_action('template_redirect', 'terms_for_autocomplete');
function terms_for_autocomplete(){
  if(isset($_GET['get_my']) && $_GET['get_my'] == 'terms'):

    $terms = &get_terms(get_taxonomies());
    foreach ($terms as $term)
      echo "{$term->name}|{$term->name} ({$term->count} results)\n";

    die();
  endif;
}
8
  • Yes, it's working. But as I type in the suggestion box I see all the tags together. Screenshot here img638.imageshack.us/img638/7216/screenshot20110108at102.png
    – Andycap
    Jan 8, 2011 at 21:21
  • well what kind of data does your autocomplete plugin expect? ...Try tags on each line: echo $mytag->name. "\n"; Jan 8, 2011 at 21:37
  • Great!! It's working. Now the problem is: - if I write a tag that exists and then I enter "," or "enter" it's ok. - If I try to write a new tag when I enter "," it put the first value of the database in the field. I think the problem is how the Javascript handles the results.
    – Andycap
    Jan 9, 2011 at 0:28
  • I had a quick look at the plugin code, and it seems the results should be in a value|title format. so change them again to echo "{$mytag->name}|{$mytag->name}\n"; and see if that fixes your problem Jan 9, 2011 at 0:47
  • 1
    I've also updated my answer to use native WP functions without SQL Jan 9, 2011 at 18:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.