Created
January 17, 2018 17:06
-
-
Save jchristopher/d3f435c0d2759dd7c0ae60197f5ad787 to your computer and use it in GitHub Desktop.
Automatically filter the_excerpt() to use SearchWP Term Highlight to find a proper global excerpt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function searchwp_term_highlight_auto_excerpt( $excerpt ) { | |
global $post; | |
if ( ! is_search() ) { | |
return $excerpt; | |
} | |
// prevent recursion | |
remove_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' ); | |
$global_excerpt = searchwp_term_highlight_get_the_excerpt_global( $post->ID, null, get_search_query() ); | |
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' ); | |
return wp_kses_post( $global_excerpt ); | |
} | |
add_filter( 'get_the_excerpt', 'searchwp_term_highlight_auto_excerpt' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment