• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / Improve Genesis Theme Search
A Telescope

Improve Genesis Theme Search

Last updated on October 3, 2017 by Sal Ferrarello

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

  1. Always use the post excerpt for the search results page
  2. Emphasize the search term in the title by wrapping the search term in a <strong> tag
  3. Emphasize the search term in the excerpt by wrapping the search term in a <strong> tag
  4. 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( ']]>', ']]&gt;', $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.

Photo Credit

MaxPixel

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Programming, Solution Tagged With: Genesis, search, WordPress Plugin

Reader Interactions

Comments

  1. Natalie says

    June 16, 2020 at 12:16 pm

    Does this code get pasted into functions.php or search.php?

    Reply
    • Sal Ferrarello says

      June 16, 2020 at 10:14 pm

      Hi Natalie,

      This code should work in either location (as well as in a plugin or in a mu-plugin).

      Reply
  2. edward I rosenthal says

    September 6, 2021 at 10:47 am

    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?

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in