Skip to content

Snippet: Convert Site Search into a Google Search

The following snippet will turn the default site search (theme or classic Widget) into a Google search. It will redirect the user to Google and run a site search with the selected keywords. This code will NOT modify the WordPress search block (it's not possible) and it will of course only show results that are indexed by Google.

// Change search action to Google
add_filter( 'wpex_search_action', function() {
	return 'https://www.google.com/search';
} );

// Add hidden search input for Google search
add_filter( 'wpex_searchform_fields', function() {
	echo '<input type="hidden" name="sitesearch" value="' . esc_url( home_url( '/' ) ) . '">';
} );

// Filter search output to modify the search query param
add_filter( 'get_search_form', function( $form ) {
	return str_replace( 'name="s"', 'name="q"', $form );
} );
All PHP snippets should be added via child theme's functions.php file or via a plugin.
We recommend Code Snippets (100% Free) or WPCode (sponsored)
Back To Top