Send Certain Searches Straight to a Page
Opens a chosen page directly when someone searches for an exact word or phrase, for example "contact" goes to your contact page and "returns" to your returns policy.
Opens WPIDE on
Code
// Search words => page to open instead of the results. Matching ignores upper/lower case. Empty = off. Examples:
// 'contact' => '/contact/',
// 'opening hours' => '/visit/#hours',
$wpide_search_shortcuts = array();
add_action( 'template_redirect', function () use ( $wpide_search_shortcuts ) {
if ( ! $wpide_search_shortcuts || ! is_search() || is_paged() ) {
return;
}
$phrase = strtolower( trim( preg_replace( '/\s+/', ' ', (string) get_search_query( false ) ) ) );
foreach ( $wpide_search_shortcuts as $words => $target ) {
if ( strtolower( trim( $words ) ) === $phrase ) {
wp_safe_redirect( preg_match( '#^https?://#i', $target ) ? $target : home_url( $target ), 302 );
exit;
}
}
} );
Notes
The list is empty by default; matching ignores capital letters and extra spaces, and only whole searches match.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.