get_search_link

The timeline below displays how wordpress function get_search_link has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 5.4

/**
 * Retrieves the permalink for a search.
 *
 * @since 3.0.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
 * @return string The search permalink.
 */
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);
}

WordPress Version: 5.3

/**
 * Retrieves the permalink for a search.
 *
 * @since  3.0.0
 *
 * @global WP_Rewrite $wp_rewrite WordPress rewrite component.
 *
 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
 * @return string The search permalink.
 */
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);
}

WordPress Version: 4.6

/**
 * Retrieves the permalink for a search.
 *
 * @since  3.0.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $query Optional. The query string to use. If empty the current query is used. Default empty.
 * @return string The search permalink.
 */
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);
}

WordPress Version: 4.4

/**
 * Retrieve permalink for search.
 *
 * @since  3.0.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $query Optional. The query string to use. If empty the current query is used.
 * @return string The search permalink.
 */
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'));
    }
    /**
     * Filter 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);
}

WordPress Version: 4.3

/**
 * Retrieve permalink for search.
 *
 * @since  3.0.0
 *
 * @global WP_Rewrite $wp_rewrite
 *
 * @param string $query Optional. The query string to use. If empty the current query is used.
 * @return string The search permalink.
 */
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 unencoded.
        $link = str_replace('%search%', $search, $permastruct);
        $link = home_url(user_trailingslashit($link, 'search'));
    }
    /**
     * Filter 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);
}

WordPress Version: 4.1

/**
 * Retrieve permalink for search.
 *
 * @since  3.0.0
 *
 * @param string $query Optional. The query string to use. If empty the current query is used.
 * @return string The search permalink.
 */
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 unencoded.
        $link = str_replace('%search%', $search, $permastruct);
        $link = home_url(user_trailingslashit($link, 'search'));
    }
    /**
     * Filter 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);
}

WordPress Version: 3.9

/**
 * Retrieve permalink for search.
 *
 * @since  3.0.0
 *
 * @param string $query Optional. The query string to use. If empty the current query is used.
 * @return string
 */
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 unencoded.
        $link = str_replace('%search%', $search, $permastruct);
        $link = home_url(user_trailingslashit($link, 'search'));
    }
    /**
     * Filter 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);
}

WordPress Version: 3.8

/**
 * Retrieve permalink for search.
 *
 * @since  3.0.0
 *
 * @param string $query Optional. The query string to use. If empty the current query is used.
 * @return string
 */
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 unencoded.
        $link = str_replace('%search%', $search, $permastruct);
        $link = home_url(user_trailingslashit($link, 'search'));
    }
    return apply_filters('search_link', $link, $search);
}

WordPress Version: 3.7

/**
* Retrieve permalink for search.
*
* @since  3.0.0
* @param string $query Optional. The query string to use. If empty the current query is used.
* @return string
*/
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 unencoded.
        $link = str_replace('%search%', $search, $permastruct);
        $link = home_url(user_trailingslashit($link, 'search'));
    }
    return apply_filters('search_link', $link, $search);
}