Created
December 17, 2020 19:42
-
-
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
This file contains hidden or 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 | |
// 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