Rectification.. you could replace the “loop through each keyword” by checking directly if the search query is a key of the redirection array.
This is the updated function “search_keyword_redirect” :
/**
* Attempt to match keywords to search query and redirect on match
*
* @since 0.1.0
*/
public function search_keyword_redirect() {
if ( ! is_search() ) {
return;
} // not a search
// get options
$redirects = get_option( 'ww_keyword_redirects' );
// check if options exist
if ( ! empty( $redirects ) ) {
if (array_key_exists(get_search_query(), $redirects)){
// redirect to url
wp_redirect( $redirects[get_search_query()][0] );
exit();
}
// no keywords matched
unset( $redirects );
return;
} else {
return; // no redirects
}
} // END search_keyword_redirect()
Regards,
Diego