Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active December 11, 2022 07:36
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jchristopher/3d46a9697b87c71b6d37963d1aa6d1fd to your computer and use it in GitHub Desktop.
Save jchristopher/3d46a9697b87c71b6d37963d1aa6d1fd to your computer and use it in GitHub Desktop.
Override SearchWP's "Did you mean?" output
<?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