Snippets: Override All Search Forms with Google Search

Below is a snippet showing how you can override all the searchforms in Total (and any WordPress theme that correctly uses the get_search_form() function). Make sure to replace 'YOUR_ENGINE_ID' and also you may have to do some additional styling to make it look good after it's implemented since the Google search will have different classnames/elements.

add_action( 'get_search_form', function() {
	ob_start(); ?>

	<script>
	(function() {
		var cx = 'YOUR_ENGINE_ID';
		var gcse = document.createElement('script');
		gcse.type = 'text/javascript';
		gcse.async = true;
		gcse.src = 'https://cse.google.com/cse.js?cx=' + cx;
		var s = document.getElementsByTagName('script')[0];
		s.parentNode.insertBefore(gcse, s);
	} )();
	</script>
	<gcse:searchresults-only></gcse:searchresults-only>

	<?php
	return ob_get_clean();
} );

Snippet Type: PHP

Posted Under: Other

All PHP snippets should be added via a child theme's functions.php file.