There are two reasons why you may want to switch Google Custom Search Engine in GeneratePress Theme.
- You need a client-side solution for a Static WordPress site.
- You want to improve the search experience by displaying highly relevant results.
Let's get straight to the point.
Follow below Steps to use CSE in GeneratePress
- Add a new page with Title "Search Results" and permalink "search" in your WordPress.
- Then, add below CSE code in the Text mode to display Search Results only.
- Sign in to CSE website
- Add a new Search Engine
- Provide the URL of site in the wildcard format (ending with *) to display it in the Search results.
- Click Create button, you will see a message: Congratulations! You've successfully created your Custom search engine.
- Next, go to Control Panel
- Go to "Look and Feel" and select Two Pages layout and click Save & Get code button.
- Important step: Click on "Search Results Details" button and set proper Search Results page URL and query parameter.
- Enable Elements module which is available in the Premium version
- Create a new Hook
- Name it 'Custom Search Engine (English)' and paste the JS that you copied in step 7.
- Place the JS code that you copied in step 9. from Google Custom Search Engine. I have added preload part myself for loading JS quickly in the browser. Make sure to replace the publisher ID.
- Use wp_head hook with highest priority value 0 to ensure script get placed at the top section of head in the HTML.
- Concerning performance, we can set Display Rules to load this JS only on WordPress Search Results page where it is actually required and non-other page.
- Similarly, using a new Hook add below inline CSS for good design of Search Results page. Below CSS Code will hide annoying advt as well in the free version.
- Generally, WordPress display Search results at
Therefore, it is important to change action path for Search box and Yoast added JSON-LD markup for the Site Search. You should place below snippet using Code Snippet plugin.
- Bonus Tip: Display Title and Heading dynamically as per user Search Query String. Thanks to James Hibbard who suggested me below snippet which is based on URLSearchParams. This should work for all modern browsers. Make sure to place script with wp_footer hook using GeneratePress Elements specifically for 'Search Results' page that we created in first step.
Now you can see Search Query in Title and Heading.
How to force Search Result link to open in same tab?
- Go to Search Features
- Click on Advanced
- Set Link Target to
_selfand save changes.
Thanks for reading.