-
Notifications
You must be signed in to change notification settings - Fork 12
Search Redirect
If there is only one search result then redirect the page to the resultant page.
The search redirect module includes the ability to redirect a search request to a specific page. You can do this with the toolbelt_search_redirect
filter as below.
function my_redirects() {
return array(
// Redirect searches for "contact" to the contact page.
array(
'term' => 'contact',
'url' => '/contact/',
),
);
}
add_filter( 'toolbelt_search_redirect', 'my_redirects' );
This filter also allows linking to a third party website, but this requires an additional change to 'allow' the third party website. The redirect uses wp_safe_redirect
and that requires external websites to be approved so that we don't redirect people to random places. An example of setting this up is below.
function my_redirects() {
return array(
// Search redirect for searches for 'bm'.
array(
'term' => 'bm',
'url' => 'https://www.binarymoon.co.uk',
),
// 404 error for 'bm.txt'.
array(
'term' => 'bm.txt',
'url' => 'https://www.binarymoon.co.uk',
),
);
}
add_filter( 'toolbelt_search_redirect', 'my_redirects' );
add_filter( 'toolbelt_404_redirect', 'my_redirects' );
function my_allowed_redirect_hosts( $hosts ) {
return array_merge(
$hosts,
array(
'www.binarymoon.co.uk',
)
);
};
add_filter( 'allowed_redirect_hosts', 'my_allowed_redirect_hosts' );
Note: This code also shows the same process being used for the 'Fast 404' module. When a file is missing you can redirect it elsewhere. The redirect array is identical, so you only need to add the relevant filter for it to be enabled.
Toolbelt is built by Ben from Pro Theme Design.