Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Created December 17, 2020 19:42
Show Gist options
  • Save jchristopher/1266c4dc759ba5a5f793a864ff0532e5 to your computer and use it in GitHub Desktop.
Save jchristopher/1266c4dc759ba5a5f793a864ff0532e5 to your computer and use it in GitHub Desktop.
Tell SearchWP to index un-hyphenated versions of hyphenated tokens as well as the hyphenated tokens
<?php
// Index non-hyphenated versions of hyphenated strings.
add_filter( 'searchwp\tokens\string', function( $string ) {
if ( ! did_action( 'searchwp\indexer\batch' ) ) {
return $string;
}
preg_match_all( '/(?=\S*[\-\_])([[:alnum:]\-\_]+)/ius', $string, $hyphenated );
if ( is_array( $hyphenated[0] ) && ! empty( $hyphenated[0] ) ) {
$string = $string . ' ' . implode( ' ', array_map( function( $token ) {
return str_replace( [ '-', '_' ], '', $token );
}, $hyphenated[0] ) );
}
return $string;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment