Posted on

Redirecting email searches to the login page

A client asked for a solution to redirect all searches for email addresses to the login page – perhaps someone is constantly using the search field when they’re actually looking for the login.

This is something that will be easier in Relevanssi Premium version 2.16, when the Redirects tool will accept regular expressions, but it’s not difficult now, either. Just add this to your theme functions.php:

add_action( 'template_redirect', 'rlv_redirect_emails' );
function rlv_redirect_emails() {
    if ( filter_var( get_search_query(), FILTER_VALIDATE_EMAIL ) ) {
        if ( wp_redirect( 'https://www.example.com/wp-login.php' ) ) {
            exit();
        }
    }
}

This function would redirect all queries that look like email addresses (based on RFC 822, with some caveats) to the login page on example.com.

There’s nothing Relevanssi-specific about this, this same function will work with any search solution that isn’t an AJAX search.

Leave a Reply

Are you a Relevanssi Premium customer looking for support? Please use the Premium support form.

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.