Support » Fixing WordPress » Highlight custom search results excerpt from Yoast

  • I am using Genesis with WordPress and I have created some functions with the help from others on the internet to make custom search results in WordPress. With these functions the search results have the Meta title and description from the Yoast SEO plug-in.

    I also made a function to hightlight the keywords that somebody used in the searchresults but it only is applying to the title and not to the excerpt that is created with the Yoast Meta description.

    This is the function for hightlight the keywords in the results:

    function wps_highlight_results($text){
         if(is_search()){
         $sr = get_query_var('s');
         $keys = explode(" ",$sr);
         $text = preg_replace('/('.implode('|', $keys) .')/iu', '<strong class="search-excerpt">'.$sr.'</strong>', $text);
         }
         return $text;
    }
    add_filter('the_excerpt', 'wps_highlight_results');
    add_filter('the_title', 'wps_highlight_results');

    I already tried to use this filter to make the highlight work in the excerpt:

    add_filter('wpseo_metadesc', 'wps_highlight_results');

    But that did not work either.

    This are the functions I use to use Yoast in the search results:

    function be_search_entry_title() {
        $title = '';
        if( function_exists( 'wpseo_replace_vars' ) ) {
            global $post;
            $title = get_post_meta( get_the_ID(), '_yoast_wpseo_title', true );
            $title = wpseo_replace_vars( $title, $post );
        }
        if( empty( $title ) ) {
            $title = get_the_title();
        }
        return $title;
    }
    
    function be_search_entry_excerpt() {
        $excerpt = '';
        if( function_exists( 'wpseo_replace_vars' ) ) {
            global $post;
            $excerpt = get_post_meta( get_the_ID(), '_yoast_wpseo_metadesc', true );
            $excerpt = wpseo_replace_vars( $excerpt, $post );
    
        }
        if( empty( $excerpt ) ) {
            $excerpt = get_the_excerpt();
        }
        return $excerpt;
    }
    
    function be_genesis_search_entry_title( $title ) {
        if( is_search() )
            $title = be_search_entry_title();
        return $title;
    }
    add_filter( 'genesis_post_title_text', 'be_genesis_search_entry_title');
    
    function be_genesis_search_entry_excerpt() {
        if( ! is_search() )
            return;
    
        remove_action( 'genesis_entry_content', 'genesis_do_post_content' );
        echo wpautop( be_search_entry_excerpt() );
    }
    add_action( 'genesis_entry_content', 'be_genesis_search_entry_excerpt', 9 );

    I think I am missing something or I have the tweak a function to also apply the highlight on the excerpt. If someone could point me in the right direction that would be great!

    • This topic was modified 1 year, 2 months ago by Jan Dembowski. Reason: Moved to Fixing WordPress, this is not an Developing with WordPress topic
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Highlight custom search results excerpt from Yoast’ is closed to new replies.