get_search_link

Retrieves the permalink for a search.

Description

(string) get_search_link( (string) $query = '' ); 

Returns (string)

The search permalink.

Parameters (1)

0. $query — Optional. (string) => ''
The query string to use. If empty the current query is used. Default empty.

Usage

  1. if ( !function_exists( 'get_search_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. The query string to use. If empty the current query is used. Default empty. 
  5. $query = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = get_search_link($query); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_search_link( $query = '' ) { 
  2. global $wp_rewrite; 
  3.  
  4. if ( empty($query) ) 
  5. $search = get_search_query( false ); 
  6. else 
  7. $search = stripslashes($query); 
  8.  
  9. $permastruct = $wp_rewrite->get_search_permastruct(); 
  10.  
  11. if ( empty( $permastruct ) ) { 
  12. $link = home_url('?s=' . urlencode($search) ); 
  13. } else { 
  14. $search = urlencode($search); 
  15. $search = str_replace('%2F', '/', $search); // %2F(/) is not valid within a URL, send it un-encoded. 
  16. $link = str_replace( '%search%', $search, $permastruct ); 
  17. $link = home_url( user_trailingslashit( $link, 'search' ) ); 
  18.  
  19. /** 
  20. * Filters the search permalink. 
  21. * @since 3.0.0 
  22. * @param string $link Search permalink. 
  23. * @param string $search The URL-encoded search term. 
  24. */ 
  25. return apply_filters( 'search_link', $link, $search );