Created
November 19, 2011 19:34
-
-
Save brandondove/1379262 to your computer and use it in GitHub Desktop.
Example usage of WordPress autocomplete functionality
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AJAX USER SEARCH | |
$('#pdsm_teacher').suggest( | |
ajaxurl+'?action=pdsm_teacher_search', | |
{ | |
delay: 500, | |
minchars: 2, | |
onSelect: function() { | |
var selected = $('ul.ac_results li.ac_over span.suggest-result-name').text(); | |
this.value = selected; | |
} | |
} | |
); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
add_action( 'wp_ajax_pdsm_teacher_search', 'teacher_search' ); | |
function teacher_search() { | |
if ( !current_user_can( 'edit_pdsm_classes' ) ) | |
die( '-1' ); | |
$s = $_GET['q']; // is this slashed already? | |
$s = trim( $s ); | |
if ( strlen( $s ) < 2 ) | |
die; // require 2 chars for matching | |
$users = get_users( array( 'search' => "*$s*", 'role' => 'pdsm_teacher' ) ); | |
foreach( $users as $user ) : | |
echo '<div class="suggest-result" style="padding: 5px 0; width: 300px;">'; | |
echo '<span class="suggest-result-img" style="float: left; padding-right: 5px;">'; | |
echo get_avatar( $user->user_email, 30 ).'</span>'; | |
echo '<span class="suggest-result-name" style="display: block;">'; | |
echo $user->user_login.'</span>'; | |
echo '<span class="suggest-result-email" style="display: block;">'; | |
echo $user->user_email.'</span>'; | |
echo '<div class="clear"></div>'; | |
echo '</div>'; | |
echo "\n"; | |
endforeach; | |
die; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment