Support » Plugin: Yoast SEO » Highlight custom search results excerpt from Yoast

  • Resolved precies

    (@precies)


    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!

Viewing 1 replies (of 1 total)
  • Plugin Support Pcosta88

    (@pcosta88)

    @precies

    Unfortunately, there’s a difference between the support we can offer and the level of programming needed to change (core) features of our plugin. Since we don’t want to take the risk that we break your website by suggesting incorrect or incomplete code, we cannot advise you on how to make such changes. And even though we cannot be of any assistance, you’re completely free to change anything in the plugin to better suit your needs.

    We highly recommend checking out our list of Yoast Partners here https://yoast.com/yoast-partners/ in case you are in need of development.

    We will also keep this issue open for 2 weeks in case someone from the community can help. Perhaps a developer watching these forums can assist further. After which, we will close in order to keep the overview.

Viewing 1 replies (of 1 total)
  • The topic ‘Highlight custom search results excerpt from Yoast’ is closed to new replies.