Override SearchWP's "Did you mean?" output
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 | |
// Override SearchWP's "Did you mean?" output. | |
class MySearchwpDidYouMean { | |
private $args; | |
function __construct() { | |
// Prevent SearchWP's automatic "Did you mean?" output. | |
add_filter( 'searchwp_auto_output_revised_search_query', '__return_false' ); | |
// Grab the "Did you mean?" arguments to use later. | |
add_action( 'searchwp_revised_search_query', function( $args ) { | |
$this->args = $args; | |
} ); | |
// Output custom "Did you mean?" message at the top of The Loop. | |
add_action( 'loop_start', function() { | |
if ( empty( $this->args ) ) { | |
return ''; | |
} | |
$phrase_query = str_replace( array( '”', '“' ), '"', SWP()->original_query ); // Accommodate curly quotes. | |
echo '<p class="searchwp-revised-search-notice">'; | |
echo wp_kses( | |
sprintf( | |
// Translators: First placeholder is the quoted search string. Second placeholder is the search string without quotes. | |
__( 'No results found for <em class="searchwp-revised-search-original">%s</em>. Showing results for <em class="searchwp-suggested-revision-query">%s</em>', 'searchwp' ), | |
esc_html( $phrase_query ), | |
esc_html( str_replace( '"', '', implode( ' ', $this->args['terms'] ) ) ) | |
), | |
array( | |
'em' => array( | |
'class' => array(), | |
), | |
) | |
); | |
echo '</p>'; | |
} ); | |
} | |
} | |
new MySearchwpDidYouMean(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment