Creating a SmartHub Results Page in WordPress
Preparing SmartHub for WordPress Integration
Before creating and configuring your WordPress page, prepare your SmartHub instance using the following steps:
Create a Custom Results Page and Settings
- If you do not yet have one, create your own custom folder which includes a custom Results page and the custom settings in your SmartHub installation directory, create one.
See How to Use the UI Builder. - These custom files enable you to customize your search pages without your customizations being overwritten by future SmartHub application upgrades.
Set Security
- Open the Admin user interface by launching your SmartHub site from its host: https://<localhost>:port/_admin.
-
Security Settings: Set SmartHub Security Settings to "None" since your end-users will not log into it to see the results - "public mode."
Security
Ensure that your SmartHub _ADMIN folder is secured so that end-users cannot reach it.
This can be done one of two ways:
- Separate the Admin and search UI from each other as 2 different IIS sites
- Deep the Admin and search UI in the same IIS site but enable Windows Authentication for the _ADMIN folder via the Authentication feature from IIS Management application.
Enable CORS
- Navigate to your SmartHub installation root directory.
- Open the file web.config for editing.
- Enable CORS by uncommenting the section for cross-domain requests, around line 312, currently.
- Change the value of the parameter
Access-Control-Allow-Original
, currently around line 314 to the value of your WordPress instance.- For example, https://myWordPressinstance.com
- For example, https://myWordPressinstance.com
- Remove the X-Frame-Options and Content-Security-Policy headers.
Create a SmartHub Search Page in WordPress
- Navigate to "All Pages" under the Pages section from the WordPress admin site.
- You must have a page on which to display your search results.
Either:- Create a new page
- Edit an existing page
- Create a new page
-
Edit the search page that you want to use.
-
Add a "Custom HTML" block to the page and copy in the code below.
-
Make sure to change the path 'smarthub.contoso.com/Results.html' to the path of your SmartHub Results (or custom Results) HTML page that you want to embed in your WordPress instance into.
Custom HTML component for search resultsCopy<iframe style="width:100%;min-height:800px;border:none;" onload="if(!this.src) this.src='https://smarthub.contoso.com/Results.html'+window.location.search + window.location.hash">
</iframe>
Because some WordPress themes have built-in search forms that are displayed on different sections of the pages, we recommend you either:
- Customize your SmartHub page to hide the searchbox that is displayed on the Results page
or
- Remove the theme search form for the WordPress search page only, and instead use the native SmartHub search page -
Configure the Search Functionality from WordPress
- Out-of-the-box WordPress redirects all searches to the root page of your instance.
- To avoid this and redirect searches to your new search page:
- Add a code snippet in the functions.php file.
- If you use a theme on your page, we recommend that you leverage the Child-theme functionality built into WordPress instead of editing the theme file functions.php.
- Read more about child themes here: https://developer.WordPress.org/themes/advanced-topics/child-themes/
How to Create a Child-Theme
These steps are optional and can be skipped
- Navigate to your WordPress Theme folder on disk.
- Create a new folder.
- Name the folder.
- Best practice is to name it [theme]-child, where [theme] is the name of the WordPress theme you are adding this functionality.
- For example: "twentytwenty-child".
-
Inside this new theme folder create a "style.css" file.
-
Append the code snippet below in the CSS file. Include the comment symbols.
-
Change
[theme]
to the name of the theme that you are adding this functionality to.-
For example: "
twentytwenty
".
Style.css contentCopy/*
Theme Name: [theme] with SmartHub Search
Theme URI: https://bainsight.com/
Description: Customization for adding SmartHub integration on top of the current theme
Author: BA Insight
Author URI: https://bainsight.com
Template: [theme]
Version: 1.0.0
License: GNU General Public License v2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
Tags: bainsight, smarthub
Text Domain: baicustomtheme
*/ -
-
Inside this new theme folder create a "functions.php" file.
- Open the file for editing.
-
Append the code snippet below into the functions.php file.
-
The snippet redirects searches to your new search page.
-
Replace
[pagename]
with the relative path to your search page, such as "/search
":Search redirect snippetCopy<?php
function change_search_url_rewrite() {
if ( is_search() && ! empty( $_GET['s'] ) ) {
wp_redirect( home_url( "[pagename]" ) . "?k=" . urlencode( get_query_var( 's' ) ) );
exit();
}
}
add_action( 'template_redirect', 'change_search_url_rewrite' );
?>
-
-
Navigate to the Themes page under the "Appearance" section on your WordPress site, under Admin view.
-
Activate the new theme that you have created.
-
The theme name, according to recommendations, is "[theme] with SmartHub Search".
-
Validate Your New Search Page
- Navigate to your WordPress instance.
- Use the built-in search box to run a search.
- If everything is set up correctly, you are redirected to the new search page you created where SmartHub results are displayed.
Note: Some custom CSS styling might be required depending on where you placed the "Custom HTML" block and how the theme is implemented.