get_search_link()WP 3.0.0

Retrieves the permalink for a search.

1 time — 0.000102 sec (fast) | 50000 times — 2.75 sec (fast)
Hooks from the function

Return

String. The search permalink.

Usage

get_search_link( $query );
$query(string)
The query string to use. If empty the current query is used.
Default: ''

Examples

0

#1 We get a link to the search page with a search query

echo get_search_link( 'something' );
// http://example.com/search/something

echo get_search_link( 'foo " bar' );
// http://example.com/search/foo+%22+bar

Notes

  • Global. WP_Rewrite. $wp_rewrite WordPress rewrite component.

Changelog

Since 3.0.0 Introduced.

get_search_link() code WP 6.5.2

function get_search_link( $query = '' ) {
	global $wp_rewrite;

	if ( empty( $query ) ) {
		$search = get_search_query( false );
	} else {
		$search = stripslashes( $query );
	}

	$permastruct = $wp_rewrite->get_search_permastruct();

	if ( empty( $permastruct ) ) {
		$link = home_url( '?s=' . urlencode( $search ) );
	} else {
		$search = urlencode( $search );
		$search = str_replace( '%2F', '/', $search ); // %2F(/) is not valid within a URL, send it un-encoded.
		$link   = str_replace( '%search%', $search, $permastruct );
		$link   = home_url( user_trailingslashit( $link, 'search' ) );
	}

	/**
	 * Filters the search permalink.
	 *
	 * @since 3.0.0
	 *
	 * @param string $link   Search permalink.
	 * @param string $search The URL-encoded search term.
	 */
	return apply_filters( 'search_link', $link, $search );
}