WordPress.org

Ready to get started?Download WordPress

Codex

Plugin API/Filter Reference/user search columns

Contents

Description

The user_search_columns filter is used to determine which user fields in the database are used when performing a search on user information.

Usage

When the 'user_search_columns' filter is called, it is passed three parameters: an array of fields to search, the search term, WP_User object

add_filter( 'user_search_columns', 'filter_function_name', 10, 3 );

function filter_function_name( $search_columns, $search, $this ) {
    // Alter $search_columns to include the fields you want to search on
    return $search_columns;
}

Where 'filter_function_name' is the function WordPress should call when filter is run. Note that the filter function must return an value after it is finished processing or the search terms will be empty.

filter_function_name should be unique function name. It cannot match any other function name already declared.

Examples

Add a field to search on

In this example the user's URL is also added to the list of columns searched

add_filter( 'user_search_columns', 'filter_function_name', 10, 3 );
    
function filter_function_name( $search_columns, $search, $this ) {
    $search_columns[] = 'user_url';
    return $search_columns;
}

Alter search fields based on search term

In this example if the user searches for something with '.com' in it only the user_url field will be searched.

add_filter( 'user_search_columns', 'filter_function_name', 10, 3 );

function filter_function_name( $search_columns, $search, $this ) {
    $val = strpos($search, '.com');
    
    if( $val !== false ) {
	    $search_columns = array('user_url');
    }
    
    return $search_columns;
}

Change Log

See Also