Skip to content

Instantly share code, notes, and snippets.

@miklb
Created April 21, 2012 03:31
Show Gist options
  • Save miklb/2433635 to your computer and use it in GitHub Desktop.
Save miklb/2433635 to your computer and use it in GitHub Desktop.
Auto Suggest WordPress hacking stuff
add_action('wp_head', 'kkf_add_autosuggest');
function kkf_add_autosuggest() {
?>
<script type="text/javascript">
// Function to add tag auto suggest
function setSuggest(id) {
jQuery('#' + id).suggest("<?php echo admin_url('/admin-ajax.php?action=ajax-tag-search&tax=search_term'); ?>", { delay: 500, minchars: 2 });
}
</script>
<?php
}
// autocompletion for non-logged-in users cribbed from http://sillybean.net/code/themes/twenty-links-a-delicious-inspired-child-theme-for-wordpress/
add_action('wp_ajax_nopriv_ajax-tag-search', 'kkf_add_autosuggest_callback');
// cribbed from admin-ajax.php
function kkf_add_autosuggest_callback() {
global $wpdb;
if ( isset( $_GET['tax'] ) ) {
$taxonomy = sanitize_key( $_GET['tax'] );
$tax = get_taxonomy( $taxonomy );
if ( ! $tax )
die( '0' );
} else {
die('0');
}
$s = stripslashes( $_GET['q'] );
if ( false !== strpos( $s, ',' ) ) {
$s = explode( ',', $s );
$s = $s[count( $s ) - 1];
}
$s = trim( $s );
if ( strlen( $s ) < 2 )
die; // require 2 chars for matching
$results = $wpdb->get_col( $wpdb->prepare( "SELECT t.name FROM $wpdb->term_taxonomy AS tt INNER JOIN $wpdb->terms AS t ON tt.term_id = t.term_id WHERE tt.taxonomy = %s AND t.name LIKE (%s)", $taxonomy, '%' . like_escape( $s ) . '%' ) );
echo join( $results, "\n" );
}
<form method="get" action="<?php get_option('home'); ?>">
<input type="text" id="s" autocomplete="off" value="type what you are looking for in this beautiful white box…" name="s" onfocus="if(this.value == 'type what you are looking for in this beautiful white box…') { this.value = ''; setSuggest('s');}" />
<input type="hidden" name="post_type" value="page" />
<input type="submit" class="button" value="Search" />
</form>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment