Over on the Yoast SEO Blog there is a great post on How to Improve WordPress Search. It is definitely worth a read (I can wait if you want to read it right now). I’m a big fan of the Genesis framework (see Why I Use the Genesis Framework) and implementing some of these changes in the Genesis framework works a little differently. Here’s a list of modifications based on this article.
Genesis Search Result Changes
- Always use the post excerpt for the search results page
- Emphasize the search term in the title by wrapping the search term in a <strong> tag
- Emphasize the search term in the excerpt by wrapping the search term in a <strong> tag
- Add a continue reading link to the excerpt
Use Excerpt for Search Results
add_action( 'genesis_before_loop', 'sk_excerpts_search_page' );
/**
* Force the search results page to display excerpts, not full content.
*
* Originally from https://gist.github.com/nutsandbolts/7377351.
*/
function sk_excerpts_search_page() {
if ( is_search() ) {
add_filter( 'genesis_pre_get_option_content_archive', function() {
return 'excerpts';
});
}
}
Emphasize the Search Term in the Title
add_filter( 'genesis_post_title_text', 'genesis_emphasize_search_result_title' );
/**
* Emphasize the search term (when present) in the title.
*
* @param string $title The post title.
* @return string The title with strong tags around the current search query.
*/
function genesis_emphasize_search_result_title( $title ) {
if ( ! is_search() ) {
return $title;
}
return emphasize( $title, get_search_query() );
}
/**
* Adds emphasis to the parts passed in $content that are equal to $search_query.
* Originally from https://yoast.com/wordpress-search/
*
* @param string $content The content to alter.
* @param string $search_query The search query to match against.
*
* @return string The emphasized text.
*/
function emphasize( $content, $search_query ) {
$keys = array_map( 'preg_quote', explode( ' ', $search_query ) );
return preg_replace( '/(' . implode( '|', $keys ) . ')/iu', '<strong class="search-excerpt">\0</strong>', $content );
}
Modify the Search Result Excerpt
Here we are going to:
- Emphasize the search term in the excerpt
- Add a continue reading link to the excerpt
add_filter( 'wp_trim_excerpt', 'custom_trim_excerpt' );
/**
* Generate custom search result excerpt.
*
* Originally from https://yoast.com/wordpress-search/
*
* @param string $text The text to be trimmed.
* @return string The trimmed text.
*/
function custom_trim_excerpt( $text = '' ) {
$text = strip_shortcodes( $text );
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]>', $text );
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$trimmed = wp_trim_words( $text, $excerpt_length, '' );
if ( is_search() ) {
$trimmed = emphasize( $trimmed, get_search_query() );
}
return $trimmed . modify_read_more_link();
}
/**
* Creates a custom read more link.
*
* Originally from https://yoast.com/wordpress-search/
*
* @return string The read more link.
*/
function modify_read_more_link() {
return ' <a class="more-link" href="' . get_permalink() . '">Continue reading</a>';
}
/**
* Adds emphasis to the parts passed in $content that are equal to $search_query.
* Originally from https://yoast.com/wordpress-search/
*
* @param string $content The content to alter.
* @param string $search_query The search query to match against.
*
* @return string The emphasized text.
*/
function emphasize( $content, $search_query ) {
$keys = array_map( 'preg_quote', explode( ' ', $search_query ) );
return preg_replace( '/(' . implode( '|', $keys ) . ')/iu', '<strong class="search-excerpt">\0</strong>', $content );
}
Combine These Modifications
I’ve combined these pieces into a single file that can be used as a WordPress plugin or as an mu-plugin
. You may have noticed the fe_emphasize()
function is used in modifying both the title and excerpt, however we only need it once in the combined file.
Additionally, I’ve added a PHP Namespace to avoid conflicts with other functions that have the same name.
I’ve made this combined file available on GitHub as the Iron Code Improve Genesis Theme Search plugin.
Does this code get pasted into functions.php or search.php?
Hi Natalie,
This code should work in either location (as well as in a plugin or in a mu-plugin).
is this also good for relvanssi having replaced the wp search function?
and does it pick up all search results or just the first one?