Skip to:
Content
Pages
Categories
Search
Top
Bottom

mention autosuggest for display names MOD


  • bigt11
    Participant

    @bigt11

    **UPDATED**
    -when multiple results show, it will show the AKA @ name properly now
    -added the same limit on the results returned that the original code used

    Like a lot of people, I want the auto suggest to look for display names and nice names. Below is my code to do just that. Im using a $wpdb->get_results to do my search. Im also changing up the displayed text to also include that users nice name so if the user is searching for “bob”. A user’s display name might be “bob9”, but there login = “I_am_bob” It will display avatar then display name then @username

    example output
    [avatar] bob9 AKA -> @i_am_bob

    Warning im not a coding master, but it seems to work for me. Below is the whole class-bplabs-autosuggest.php so just make a backup of your site, and overwrite inside class-bplabs-autosuggest.php with the code below

    <?php
    /**
    * @package BP_Labs
    * @subpackage Mentions_Autosuggest
    */
    
    // Exit if accessed directly
    if ( !defined( 'ABSPATH' ) )
    exit;
    
    /**
    * Implements the <a href='http://buddypress.org/community/members/message/' rel='nofollow'>@message</a> autosuggest experiment.
    *
    * <a href='http://buddypress.org/community/members/since/' rel='nofollow'>@since</a> 1.0
    */
    class BPLabs_Autosuggest extends BPLabs_Beaker {
    /**
    * Enqueue javascript
    *
    * <a href='http://buddypress.org/community/members/since/' rel='nofollow'>@since</a> 1.0
    */
    public function enqueue_script() {
    $dir = WP_PLUGIN_URL . '/bp-labs/beakers/js/jquery.mentions';
    
    if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG )
    wp_enqueue_script( 'bplabs-autosuggest-js', "{$dir}.dev.js", array( 'jquery' ), '1.2' );
    else
    wp_enqueue_script( 'bplabs-autosuggest-js', "{$dir}.js", array( 'jquery' ), '1.2' );
    
    wp_localize_script( 'bplabs-autosuggest-js', 'BPMentions', array(
    'error1'     => __( 'Sorry, an error occurred.', 'bpl' ),
    'error2'     => _x( 'Please try again.', 'an error occurred', 'bpl' ),
    'searching'  => _x( 'Searching for members to mention... one moment please...', 'started a search', 'bpl' )
    ) );
    
    // This bit of javascript is what you could add directly to your theme
    $dir = WP_PLUGIN_URL . '/bp-labs/beakers/js/autosuggest';
    
    if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG )
    wp_enqueue_script( 'bplabs-autosuggest-theme-js', "{$dir}.dev.js", array( 'bplabs-autosuggest-js' ), '1.0' );
    else
    wp_enqueue_script( 'bplabs-autosuggest-theme-js', "{$dir}.js", array( 'bplabs-autosuggest-js' ), '1.0' );
    }
    
    /**
    * Enqueue CSS
    *
    * <a href='http://buddypress.org/community/members/since/' rel='nofollow'>@since</a> 1.0
    */
    public function enqueue_style() {
    $dir = WP_PLUGIN_URL . '/bp-labs/beakers/css/jquery.mentions';
    
    if ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG )
    wp_enqueue_style( 'bplabs-autosuggest', "{$dir}.dev.css", array(), '1.0' );
    else
    wp_enqueue_style( 'bplabs-autosuggest', "{$dir}.css", array(), '1.0' );
    }
    
    /**
    * Add the AJAX callback for the mention_autosuggest() method.
    *
    * <a href='http://buddypress.org/community/members/since/' rel='nofollow'>@since</a> 1.0
    */
    protected function register_actions() {
    add_action( 'wp_ajax_activity_mention_autosuggest', array( 'BPLabs_Autosuggest', 'mention_autosuggest' ) );
    }
    
    /**
    * AJAX receiver for the @mention autosuggest jQuery library. Performs a search on the string provided and returns matches.
    *
    * <a href='http://buddypress.org/community/members/global/' rel='nofollow'>@global</a> object $bp BuddyPress global settings
    * @return mixed Either HTML or JSON. If error, "-1" for missing parameters, "0" for no matches.
    * <a href='http://buddypress.org/community/members/see/' rel='nofollow'>@see</a> bp-labs/beakers/js/jquery.mentions.dev.js
    * <a href='http://buddypress.org/community/members/since/' rel='nofollow'>@since</a> 1.0
    */
    public function mention_autosuggest() {
    global $bp;
    
    if ( empty( $_POST ) || empty( $_POST ) )
    exit( '-1' );
    
    // Sanitise input
    $search_query = implode( '', (array) preg_replace( array( '|^https?<img src="smileys/irritated.gif" width="" height="" alt=":/" title=":/" class="bbcode_smiley" />/|i', '|*|', '|@|' ), '', $_POST ) );
    if ( empty( $search_query ) )
    exit( '-1' );
    
    //$args = array(
    //	'count_total' => false,
    //	'number'      => (int) $_POST,
    //	'search'      => "{$search_query}*"
    //);
    
    if ( !empty( $bp->loggedin_user->id ) )
    $args = array( $bp->loggedin_user->id );
    
    if ( bp_is_username_compatibility_mode() ) {
    //$args  = array( 'ID', 'user_login' );
    //$args = 'login';
    
    }
    else {
    //$args  = array( 'ID', 'display_name' );
    //$args = 'nicename';
    }
    
    $args = apply_filters( 'bpl_mention_autosuggest_args', $args );
    
    // Search users
    //TAO custom search using display name
    global $wpdb;
    $TAO_limit_results = (int) $_POST;
    $user_search_results = $wpdb->get_results( "SELECT ID, user_nicename, display_name, user_login FROM wp_users WHERE user_login like'%$search_query%' OR display_name like '%$search_query%' LIMIT 0, $TAO_limit_results" );
    //print_r($user_search_results);
    echo "<br>";
    echo 'Searching for members to mention with "' .$search_query .'" in their name.';
    echo "<br>";
    echo 'Click on the user you wish to mention.';
    $args = array(
    'search'      => "*{$search_query}*"
    
    );
    //$user_search_results = new WP_User_Query( $args );
    //print_r($user_search_results);
    if ( empty( $user_search_results ) ) {
    
    // Return JSON
    if ( !empty( $_POST ) && 'json' == $_POST ) {
    exit( json_encode( false ) );
    
    // Return HTML
    } else {
    printf( '<li class="section error"><span>%s</span> %s
    </li>', _x( 'No matches found.', 'no search results', 'bpl' ), _x( 'Please check your spelling.', 'no search results', 'bpl' ) );
    exit();
    }
    }
    
    // If logged in, get user's friends
    $friend_ids = array();
    if ( !empty( $bp->loggedin_user->id ) && bp_is_active( 'friends' ) )
    $friend_ids = friends_get_friend_user_ids( $bp->loggedin_user->id );
    
    $search_results = array( 'friends' => array(), 'others' => array() );
    
    // Build results
    foreach ( (array) $user_search_results as $user ) {
    $result         = new stdClass;
    $result->avatar = bp_core_fetch_avatar( array( 'item_id' => $user->ID, 'width' => 30, 'height' => 30, 'type' => 'thumb', 'alt' => __( 'Profile picture of %s', 'bpl' ) ) );
    $result->name   = bp_core_get_user_displayname( $user->ID );
    //TAO adding the nicename to do a AKA
    $result->TAO_user_nicename = $user->user_nicename;
    
    if ( bp_is_username_compatibility_mode() )
    $result->id = $user->user_login;
    else
    $result->id = $user->user_nicename;
    
    if ( in_array( $user->ID, (array) $friend_ids ) )
    $search_results[] = $result;
    else
    $search_results[]  = $result;
    }
    
    apply_filters_ref_array( 'bpl_mention_autosuggest', array( &$search_results, $args ) );
    
    // Return JSON
    if ( !empty( $_POST ) && 'json' == $_POST ) {
    exit( json_encode( $search_results ) );
    
    // Return HTML
    } else {
    $html = array();
    
    foreach ( $search_results as $section => $items ) {
    if ( empty( $items ) )
    continue;
    
    // Friends and other users
    if ( 'friends' == $section || 'others' == $section ) {
    if ( 'friends' == $section ) {
    $html[] = sprintf( '<li class="section friends">%s
    </li>', __( 'Your friends', 'bpl' ) );
    
    } elseif ( 'others' == $section ) {
    if ( !empty( $search_results ) )
    $html[] = sprintf( '<li class="section other">%s
    </li>', sprintf( __( 'Other people on %s', 'bpl' ), get_bloginfo( 'name', 'display' ) ) );
    else
    $html[] = sprintf( '<li class="section other">%s
    </li>', sprintf( __( 'People on %s', 'bpl' ), get_bloginfo( 'name', 'display' ) ) );
    }
    
    foreach ( $items as $item )
    $html[] = sprintf( '<li class="%s">%s
    </li>', esc_attr( $item->id ), $item->avatar . esc_html( $item->name ) . '&nbsp;&nbsp&nbsp;&nbsp;AKA&nbsp;&nbsp;---> @' .$item->TAO_user_nicename );
    
    // For third-party extensions
    } else {
    $custom_section = apply_filters( 'bpl_mention_autosuggest_custom_section', false, $section, $items );
    
    if ( !empty( $custom_section ) )
    $html = array_merge( $html, (array) $custom_section );
    }
    }
    
    // Safety net
    if ( empty( $html ) )
    $html[] = sprintf( '<li class="section error"><span>%s</span> %s
    </li>', _x( 'No matches found.', 'no search results', 'bpl' ), _x( 'Please check your spelling.', 'no search results', 'bpl' ) );
    
    exit( apply_filters( 'bpl_mention_autosuggest_html', implode( PHP_EOL, $html ), $html ) );
    }
    }
    }
    new BPLabs_Autosuggest();
    ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘mention autosuggest for display names MOD’ is closed to new replies.
Skip to toolbar