Skip to:
Content
Pages
Categories
Search
Top
Bottom

Display Name Autosuggest

  • Avatar of itchesavvy
    itchesavvy
    Member

    @itchesavvy

    I tried to reply back to @Matt post, but kept getting an error that “I already said that”. So sorry to start another topic but I thought this would be useful.

    @Matt here is what I did and it works for me. I’m interested in expanding this in several ways, @djpaul suggested I post my success here. I’m not sure how to proceed in general with improving this further, but I will try to keep posting as I work out better solutions. I’d really like to break out from the labs at this point and create a stand alone plugin that handles @mention functionality exclusively but again I’m just learning the ropes.

    Here is how I did it based off an add-in Daniel Bachhuber had created for author lookups in WordPress posts.

    First add this to your theme’s function.php file:

    function db_filter_user_query( &$user_query ) {
    if ( is_object( $user_query ) )
    $user_query->query_where = str_replace( “user_nicename LIKE”, “display_name LIKE”, $user_query->query_where );
    return $user_query;
    }

    Then here is the modified code from the bp-labs/beakers/class-bplabs-autosuggest.php file:
    \
    add_filter( ‘pre_user_query’, ‘db_filter_user_query’ );

    // Sanitise input
    $search_query = implode( ”, (array) preg_replace( array( ‘|^https?://|i’, ‘|*|’, ‘|@|’ ), ”, $_POST ) );
    if ( empty( $search_query ) )
    exit( ‘-1′ );

    $args = array(
    ‘count_total’ => false,
    ‘number’ => (int) $_POST,
    ‘search’ => “{$search_query}*”,
    ‘search_fields’ => ‘display_name’
    );

    if ( !empty( $bp->loggedin_user->id ) )
    $args = array( $bp->loggedin_user->id );

    if ( bp_is_username_compatibility_mode() ) {
    $args = array( ‘ID’, ‘user_login’ );
    $args = ‘user_login';

    } else {
    $args = array( ‘ID’, ‘display_name’ );
    $args = ‘display_name';
    }

    $args = apply_filters( ‘bpl_mention_autosuggest_args’, $args );

    // Search users
    $user_search_results = get_users( $args );

    remove_filter( ‘pre_user_query’, ‘db_filter_user_query’ );
    \

Viewing 6 replies - 1 through 6 (of 6 total)
  • Thanks for sharing. I’m sure someone will find this very helpful :)

    Avatar of itchesavvy
    itchesavvy
    Member

    @itchesavvy

    No problem. @djpaul is there a way to incorporate the add_filter and remove_filter into the apply_filters statement $args = apply_filters( ‘bpl_mention_autosuggest_args’, $args );

    If so, what would be the correct syntax.

    Just noticed an error in the code I posted. I copied and pasted from an old version.

    Please change this section to as follows:
    } else {
    $args = array( ‘ID’, ‘user_login’, ‘display_name’ );
    $args = ‘display_name';
    }

    This insures when you select the display name it properly writes the @user_login to the box. Right now I am trying to figure out how to create a filter to convert all the @user_login text to Display_name so that the user experience will be to see the display_names, but the database will still use the @user_login. This unfortunately will not work though when authoring the actual status update, without some special means (javascript?)

    Avatar of Manish
    Manish
    Participant

    @financetrain

    @itchesavvy I edited my code as per the modification above. The Autosuggest works fine with the display name, however when I click on one of the results, nothing gets written in the box. Only a space comes after the @ sign. Can you please help with the problem?

    Avatar of Manish
    Manish
    Participant

    @financetrain

    My problem has got resolved.

    Changed the following:
    if ( bp_is_username_compatibility_mode() ) {
    $result->id = $user->user_login;
    }
    else {
    $result->id = $user->user_login;

    }

    So, basically no if/else condition and in either case used $user->user_login

    Avatar of 83 Oranges
    83 Oranges
    Participant

    @umagokhale

    Guys, here’s how to fix BP @ Mentions & Display Names Conflict: http://customwebdesignseo.com/fixing-bp-mentions-display-names-conflict

    Avatar of Philipp
    Philipp
    Participant

    @philippmuenchen

    Wow! I would love to use this, too. But I’m not sure which part of the bp-labs/beakers/class-bplabs-autosuggest.php file I have to replace. Could you please post the code of the whole file after you changed it? That would be great…

    Searching for usernames is just confusing at the moment…

    Thanks a lot!

    @umagokhale Thank you for the link. But that’s not realy helpful.

Viewing 6 replies - 1 through 6 (of 6 total)

The forum ‘BP Labs’ is closed to new topics and replies.