Javascript API
The javascript API consists of two parts, one is the events triggered by the plugin, and methods, which you can use to trigger specific events.

Events list

The events are fired on the main search container element which you can target with ".asp_main_container" class selector. For example attaching an event handler before the search starts:
1
$(".asp_main_container").on("asp_search_start", function(event, id, instance, phrase) {
2
console.log(id, instance, phrase);
3
});
Copied!

Common arguments

All of the events will always pass on these three arguments (+ additional if defined):
  • event (object) - a common javascript event object, always passed in event handlers
  • id (integer) - the search ID. This is the same as the one in the shortcode.
  • instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.
Triggers when the search search instance is initialized.

asp_settings_show

Triggers when the search settings are displayed.

asp_settings_hide

Triggers when the search settings get hidden.

asp_results_show

Triggers when the search results are displayed.

asp_results_hide

Triggers when the search results get hidden.

asp_elementor_results

Triggers when the search settings are displayed.

Additional arguments

  • element (Element) - The node object

asp_search_start

Triggers right before the search is started.

Additional arguments

  • s (string) - The search phrase

asp_search_end

Triggers right after the search is finished.

Additional arguments

  • s (string) - The search phrase
  • data (string) - HTML data returned by the server

Sample implementation to the functions.php file in theme/child theme directory

1
add_action('wp_footer', 'asp_api_sample_code');
2
function asp_api_sample_code() {
3
?>
4
<script>
5
jQuery(function($){
6
$(".asp_main_container").on("asp_search_end", function(){
7
console.log(id, instance, phrase);
8
});
9
});
10
</script>
11
<?php
12
}
Copied!

Triggering Events/Functions

To call a pre-defined event, use either version of the following function:
1
ASP.api( id, function_name);
2
ASP.api( id, instance, function_name);
3
ASP.api( id, function_name, args);
4
ASP.api( id, instance, function_name, args);
Copied!

Function arguments

  • id (integer) - the search ID. This is the same as the one in the shortcode. If id=0, then the call applies to all visible search instances on the page.
  • instance (integer) - the instance number of the current search ID. This will help if the same search shortcode is used multiple times on the same page.
  • function_name - the event/function you want to trigger
  • args - arguments passed to the triggered function

Functions list

searchFor

Triggers the search for a specific search phrase.

Arguments

  • s (string) - the search phrase

Examples

1
// On all instances on search ID=1
2
ASP.api(1, "searchFor", "search phrase");
3
4
// On instance 2 of search ID=1
5
ASP.api(1, 2, "searchFor", "search phrase");
Copied!

searchRedirect

Triggers the search redirection to the results page (as configured on the back-end) using the given search phrase.

Arguments

  • s (string) - the search phrase

Examples

1
// On all instances on search ID=1
2
ASP.api(1, "searchRedirect", "search phrase");
3
4
// On instance 2 of search ID=1
5
ASP.api(1, 2, "searchRedirect", "search phrase");
Copied!

toggleSettings

Toggles the search settings container state to either visible or invisible.

Arguments

  • state (string) - 'opened' or 'closed'

Examples

1
// On all instances on search ID=1
2
ASP.api(1, "toggleSettings", "opened");
3
4
// On instance 2 of search ID=1
5
ASP.api(1, 2, "toggleSettings", "closed");
Copied!

closeResults

Closes the results box

Arguments

  • clear (bool) - Clears the search input, when true. Defaults to false.

Examples

1
// On all instances on search ID=1
2
ASP.api(1, "closeResults");
3
// On instance 2 of search ID=1, also clears the search input
4
ASP.api(1, 2, "closeResults", true);
Copied!

getStateURL

Returns a URL for the currently viewed page, with the search settings preset to the current state. Visiting this URL will preset the search settings to that state.
You can learn more about this function here.

Examples

1
// On instance 2 of search ID=1 - the instance
2
ASP.api(1, 2, "closeResults", true);
Copied!

Examples

Search for a specific phrase on search ID 1:
1
ASP.api(1, "searchFor", "search phrase");
Copied!
Similarly, but only on instance 1:
1
ASP.api(1, 1, "searchFor", "search phrase");
Copied!
Search all visible instances:
1
ASP.api(0, "searchFor", "search phrase");
Copied!

Settings

Toggle search settings box on/off:
1
ASP.api(1, "toggleSettings");
2
3
// ..or specific instance with the same search ID only
4
ASP.api(1, 2, "toggleSettings");
Copied!
Explicitly show/hide the settings:
1
// show
2
ASP.api(1, "toggleSettings", "show");
3
4
// hide
5
ASP.api(1, "toggleSettings", "hide");
6
7
// show-hide setting of all visible instances
8
ASP.api(0, "toggleSettings", "show");
9
ASP.api(0, "toggleSettings", "hide");
Copied!

Results

To hide the results window:
1
ASP.api(1, "closeResults");
2
3
// close all open result boxes
4
ASP.api(0, "closeResults");
5
6
// ..or specific instance with the same search ID only
7
ASP.api(1, 2, "closeResults");
Copied!

Current search state URL - presetting the search filters and input to a specific state

There is a way to get the current filters and search input state on the current page, and generate a URL - which presets the search to this state.
1
// Get the current state of the search filters on search ID=1, and first printed instance
2
ASP.api(1, 1, 'getStateURL')
Copied!
This returns a string like:
1
https://yoursite.com/?p_asid=1&p_asp_data=1&current_page_id=1041&woo_currency=USD&qtranslate_lang=0&filters_changed=0&filters_initial=1&termset%5Bcategory%5D%5B%5D=81&termset%5Bcategory%5D%5B%5D=82&termset%5Bcategory%5D%5B%5D=83&termset%5Bcategory%5D%5B%5D=98&termset%5Bcategory%5D%5B%5D=147&termset%5Bcategory%5D%5B%5D=103&termset%5Bcategory%5D%5B%5D=102&termset%5Bcategory%5D%5B%5D=100&termset%5Bcategory%5D%5B%5D=84&termset%5Bcategory%5D%5B%5D=92&termset%5Bcategory%5D%5B%5D=95&termset%5Bcategory%5D%5B%5D=94&termset%5Bcategory%5D%5B%5D=93&termset%5Bcategory%5D%5B%5D=96&termset%5Bcategory%5D%5B%5D=86&termset%5Bcategory%5D%5B%5D=87&termset%5Bcategory%5D%5B%5D=85&asp_gen%5B%5D=title&asp_gen%5B%5D=content&asp_gen%5B%5D=excerpt&post_date_from_real=31-01-2011&post_date_from=2011-01-31&post_date_to_real=23-12-2020&post_date_to=2020-12-23&customset%5B%5D=post&customset%5B%5D=page"
Copied!
..visiting this URL will preset the search filters to this state.
Last modified 1mo ago