Warning: This article describes configuration for legacy ready-made views. Those views have been replaced with modern, easy-to-configure analogues and we strongly recommend using them instead.
See the version of the article for newest views.
You can adjust the functionality of the Search Widget with a global JavaScript variable called “addsearch_settings”. The variable must be defined above the script tag loading AddSearch JavaScript snippet.
<!-- AddSearch settings --> <script> window.addsearch_settings = { default_sortby: 'date' } </script> <!-- This script must be below addsearch_settings --> <script src="https://addsearch.com/js/?key=####"></script>
Currently, addsearch_settings supports the following settings:
Setting | Values | Description |
---|---|---|
allow_parent_form_submit | true, null | If "true", pressing enter in a search input field inside a form element, submits the form |
date_format_function | JavaScript function | Custom JavaScript function to format publishing dates in search results. The function is called with Date object as a parameter |
default_sortby | date relevance (default) | Default order of results |
direction | rtl, ltr | Enable right-to-left UI by setting the value to rtl |
display_date | true false (default) | Display the publishing date of search results instead of category |
ga_tracking_id | UA-xxxxxx-x | Google Analytics tracking ID. Needed if Global site tag (gtag) in use |
jwt | String | JSON Web Token to pass with search queries when search index is protected |
link_target | _blank, _self, _top, _parent | Window or a frame where the clicked results link should open. E.g. _blank to open links in a new tab. |
placeholder | String (e.g. "Search..") | Placeholder text to show in the search field |
results_box_css_classname | String (custom CSS class) | CSS class name added to #addsearch-results div. Practical for A/B testing and such |
results_box_opening_direction | left, right, center | Define if the results box opens to the left-hand or right-hand side below the search field. By default, the value is calculated automatically |
show_search_suggestions | true, false (default) | Show search suggestions on the right column of the widget (separate configuration needed on Dashboard) |
To change the sort of search results, call the following function:
addsearch.sortBy(value);
An example of the implementation:
<script> function changeSort(event, sort) { // Prevent Widget from closing (event || window.event).stopPropagation(); addsearch.sortBy(sort); } </script> Sort by: <button onclick="changeSort(event, 'relevance');">Relevance</button> <button onclick="changeSort(event, 'date');">Date</button>
We’re always happy to help with code or other questions you might have. Search our documentation, contact support, or connect with our sales team.