Developer Reference

Developer Reference

This page is for developers extending or customising the HawkSearch plugin. The same information is summarised on the plugin's HawkSearch → Developers page.

🚧

Don't edit the plugin's files

Updates replace the plugin's files. Make customisations with the filters on this page, in your theme's functions.php or a small custom plugin.

Where the Code Lives

PartWhere
Rapid UINot bundled. Version 7.0.3 is loaded from the jsDelivr CDN, pinned so a new release never changes a live site by surprise: https://cdn.jsdelivr.net/npm/@bridgeline-digital/[email protected]/dist/hawksearch-handlebars-ui.min.js
Rapid UI settingsPrinted in the page head as a HawkSearch.init() call, built in hawksearch/includes/class-search.php. On pages that only need a recommendation widget or tracking, it is printed in the footer.
Indexingincludes/class-items.php (what is sent per item) and includes/class-indexer.php (reindex, schedule, real-time updates).
Fields and facetsincludes/class-fields.php and includes/class-facets.php (HawkSearch Dashboard API).
Trackingincludes/class-tracking.php (product views, add to cart and purchases).
Recommendationsincludes/class-recommendations.php.

Rapid UI is loaded on the results page; on every page once site search is on; and on any page with a recommendation widget or a tracked event (product views, cart, order confirmation).

Only the Client GUID and the public endpoints reach the browser. The API key is used only on the server.

Configuration Constants

Settings can be defined in wp-config.php, for example to keep the API key out of the database or to use different engines per environment. A constant overrides the value on the Settings page, and the field is shown as locked.

define( 'HAWKSEARCH_API_KEY', 'your-api-key' );
define( 'HAWKSEARCH_CLIENT_GUID', 'your-client-guid' );
define( 'HAWKSEARCH_ENGINE', 'yourengine' );
define( 'HAWKSEARCH_ENVIRONMENT', 'prod' ); // dev, test or prod

Shortcodes

ShortcodeWhat it does
[hawksearch_results]Search results, facets, tabs, sorting and paging. Goes on the results page.
[hawksearch_search_field]A search box with autocomplete, anywhere.
[hawksearch_recommendations widget="..."]A recommendation widget. Optional item="123" sets the item; on a single product, post or page the current item is used automatically.

Filters

Search and Rapid UI

FilterArgumentsUse
hawksearch_rapid_ui_configarray $configChange the HawkSearch.init() settings: field mappings, component templates, strings, styles.
hawksearch_search_field_htmlstring $html, string $styleChange the search box markup, for example for Smart Search.
hawksearch_rapid_ui_urlstring $urlLoad another Rapid UI version, or a copy on your own server.

The default field mappings are:

Rapid UIHawkSearch field
titletitle
urlurl
imageUrlimage_url
descriptionexcerpt, then content
priceregular_price
salePricesale_price
sku, brand, ratingsku, brand, rating
typetype (item for products, content otherwise)

Indexing

FilterArgumentsUse
hawksearch_post_typesarray $typesChange which content types are indexed.
hawksearch_excluded_idsarray $idsKeep particular posts, pages or products out of HawkSearch.
hawksearch_indexablebool $indexable, WP_Post $postDecide per item whether it is indexed.
hawksearch_index_itemarray $item, WP_Post $postChange or add values before an item is sent.
hawksearch_item_typestring $type, WP_Post $postSet an item's type: item (shown as a product) or content. Useful for a custom product post type.
hawksearch_tab_valuestring $tab, WP_Post $postChange the results tab an item appears under.
hawksearch_detected_fieldsarray $fields, array $typesChange the fields found by Detect Fields.
hawksearch_skip_meta_keysarray $keysCustom field keys that Detect Fields should ignore.

Items are sent as field name: array of values, for example:

{
  "id": ["1600"],
  "title": ["Women's Dree Jacket"],
  "url": ["https://example.com/product/womens-dree-jacket/"],
  "image_url": ["https://example.com/wp-content/uploads/dree-jacket.jpg"],
  "type": ["item"],
  "tab": ["Products"],
  "price": [89],
  "category": ["Jackets"],
  "brand": ["Prana"]
}

Use Indexing → Preview an item to see exactly what is sent for any item.

Examples

Rename a Results Tab

add_filter( 'hawksearch_tab_value', function ( $tab, $post ) {
	return 'post' === $post->post_type ? 'Blog Posts' : $tab;
}, 10, 2 );

Run a reindex afterwards, and update the facet's Default Item Type in the Workbench if you rename the Products tab.

Add a Value to Every Item

The field must exist in HawkSearch (create it in the Workbench, or add it as a custom field and sync it on the Field Configuration page).

add_filter( 'hawksearch_index_item', function ( $item, $post ) {
	$item['region'] = array( get_post_meta( $post->ID, 'region', true ) ?: 'All' );
	return $item;
}, 10, 2 );

Using Smart Search

Use the unified Smart Search box everywhere the plugin places a search box. Smart Search must be enabled for your engine first.

add_filter( 'hawksearch_search_field_html', function () {
	return '<div class="hawksearch-search-box"><hawksearch-unified-search-field></hawksearch-unified-search-field></div>';
} );

For Smart Response, add <hawksearch-smart-response></hawksearch-smart-response> in a Custom HTML block above the results shortcode. See Available Enhancements.

Changing the Look of Results

Each Rapid UI component has a Handlebars template you can replace (Rapid UI Overrides). Copy the component's default markup from the component reference into a template tag on the page, change it, and point the component at it:

add_filter( 'hawksearch_rapid_ui_config', function ( $config ) {
	$config['components']['search-results-item']['template'] = 'my-result-item';
	return $config;
} );

add_action( 'wp_footer', function () {
	echo '<template id="my-result-item">';
	echo '<!-- your copy of the search-results-item markup -->';
	echo '</template>';
} );

To hide a component, set its template to an empty string.

Adding Custom Styles

add_filter( 'hawksearch_rapid_ui_config', function ( $config ) {
	$config['css']['customStyles'][] = '.search-results-list__item__title { font-weight: 700; }';
	return $config;
} );

Rapid UI components use shadow DOM, so styles passed this way reach inside them; normal theme CSS styles only the page around them.

Tracking Events in the Browser

With verbose logging on (HawkSearch → Tracking), every event is written to the browser console, prefixed [HawkSearch tracking]. The plugin calls Rapid UI's tracking service, so all events share Rapid UI's visitor and visit IDs:

EventRapid UI call
Product viewHawkSearch.services.tracking.trackPageLoad(1, productId)
Add to carttrackAddToCart(productId, quantity, price, currency)
PurchasetrackOrder(orderNumber, items, subTotal, tax, total, currency)

To track a custom event, wait for Rapid UI and use the same service:

addEventListener( 'hawksearch:initialized', function () {
	HawkSearch.services.tracking.trackPageLoad( 5 ); // 5 = other page type
} );

See the Event Tracking API for event types.

Scheduled Tasks

HookWhat it does
hawksearch_scheduled_reindexThe scheduled full reindex (WP-Cron).
hawksearch_index_tickRuns reindex batches in the background. With WooCommerce, batches use Action Scheduler (group hawksearch).
hawksearch_sync_itemsSends real-time updates to HawkSearch.

With WP-CLI, wp cron event run hawksearch_scheduled_reindex starts the scheduled reindex immediately.

Stored Settings

The plugin stores its settings in WordPress options whose names start with hawksearch_, for example hawksearch_settings (connection), hawksearch_fields (Field Configuration), hawksearch_indexing, hawksearch_search, hawksearch_tracking and hawksearch_recommendations. Facets and recommendation widgets are stored in HawkSearch, not in WordPress.


Did this page help you?