Skip to content

Instantly share code, notes, and snippets.

@Rayken
Last active December 18, 2015 07:29
Show Gist options
  • Save Rayken/5746924 to your computer and use it in GitHub Desktop.
Save Rayken/5746924 to your computer and use it in GitHub Desktop.
<?php
/* highlight keywords */
function my_highlight_search( $text ) {
if( is_search() ){
$keys = implode( '|', explode( ' ', get_search_query() ) );
$text = preg_replace( '/(' . preg_quote ( $keys ) .')/iu', '<span class="search-term">\0</span>', $text );
}
return $text;
}
add_filter( 'the_title', 'my_highlight_search' );
add_filter( 'the_content', 'my_highlight_search' );
add_filter( 'the_excerpt', 'my_highlight_search' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment