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
1 Answer
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;
}