List of the plugin php hooks.
Hook | Type | Description |
---|---|---|
aws_meta_keys | filter | Admin hook. Final array of meta fields for the settings page. |
aws_meta_keys_include | filter | Admin hook. Add to the array of meta fields additional fields. |
aws_meta_keys_unfiltered | filter | Admin hook. Array of meta fields for the settings page before filtering. |
aws_admin_page_options | filter | Admin hook. Filter options array for the plugin settings page. |
aws_before_strip_shortcodes | filter | Index table hook. Filter product strings before stripping shortcodes. |
aws_special_chars | filter | Special characters to remove from the product strings. |
aws_diacritic_chars | filter | Filters array of diacritic chars. |
aws_normalize_string | filter | Filters products normalized strings. |
aws_synonyms_option_array | filter | Filters synonyms array before adding them to the index table. |
aws_front_filters | filter | Array of search form filters before output. |
aws_front_data_parameters | filter | Filter search form settings. |
aws_searchbox_markup | filter | Filter search form HTML output. |
aws_search_page_filters | filter | Active products filters of current search results page. |
aws_products_search_page_filtered | filter | Product on search results page after filters apply. |
aws_products_order_by | filter | Filter order by value for search results page. |
aws_products_order | filter | Product on search results page after the ordering. |
aws_index_posts_per_page | filter | Index table hook. Number of products to be indexed per iteration. |
aws_index_cron_runner_time_limit | filter | Index table hook. Maximum execution time for the index script. |
aws_index_max_cron_attemps | filter | Index table hook. Max number of the index script repeats before failing. |
aws_index_product_ids | filter | Index table hook. Array of products IDs that will be indexed. |
aws_index_apply_filters | filter | Index table hook. Apply or not standard content filters for indexed data. |
aws_indexed_title | filter | Index table hook. Filters product title before it will be indexed. |
aws_indexed_content | filter | Index table hook. Filters product content before it will be indexed. |
aws_indexed_excerpt | filter | Index table hook. Filters product short description before it will be indexed. |
aws_indexed_custom_fields | filter | Index table hook. Filters product custom fields before they will be indexed. |
aws_indexed_data | filter | Index table hook. Filters product data before adding to the index table. |
aws_create_index_table_sql | filter | Index table hook. Filters SQL query that will be using to create index table. |
aws_extracted_string | filter | Index table hook. Filters extracted from the product data strings before index. |
aws_extracted_terms | filter | Index table hook. Filters array of product terms before index. |
aws_page_results | filter | Total number of search results for search results page. |
aws_posts_per_page | filter | Number of search results per page for search results page. |
aws_searchpage_enabled | filter | Display or not plugin results for current search results page. |
aws_search_page_results | filter | Array of results for search results page. |
aws_search_page_query | filter | Search query string for search results page. |
aws_search_results_products_ids | filter | Array of product IDs to display as search results. |
aws_search_results_products | filter | Filter products search results. |
aws_search_results_tax_archives | filter | List of enabled for search taxonomies archive pages. |
aws_search_results_all | filter | Filter all search results before displaying. |
aws_search_terms | filter | Filters array of search terms before generating SQL query. |
aws_tax_filter_include_childs | filter | Include or not child terms for taxonomies filter. |
aws_exclude_products | filter | Exclude certain products from the search results. |
aws_search_current_lang | filter | Products language to search for. |
aws_search_query_array | filter | Filter search query parameters. |
aws_search_query_string | filter | Filter search query SQL string. |
aws_title_search_result | filter | Product title inside search results list. |
aws_excerpt_search_result | filter | Product excerpt inside search results list. |
aws_search_pre_filter_products | filter | Filter products array before the output. |
aws_image_size | filter | Size of product images inside search results block. |
aws_highlight_tag | filter | Tag to use for highlighting search words inside the content. |
aws_tax_search_data | filter | Filters the array of taxonomies search data. |
aws_search_terms_number | filter | Maximal number of taxonomies archive pages inside search results. |
aws_terms_search_query | filter | Filter taxonomies archive pages SQL query string. |
aws_search_tax_results | filter | Filter taxonomies archive pages results. |
aws_search_terms_description | filter | Search or not inside the taxonomy terms description. |
aws_search_tax_exclude | filter | Exclude certain taxonomies terms from the search results. |
aws_terms_exclude_$taxonomy_name | filter | Exclude certain terms archive pages from the search results. |
aws_users_search_data | filter | Filters the array of users search data. |
aws_users_search_args | filter | Filter users search query arguments. |
aws_users_search_query | filter | Filter users archive pages SQL query string. |
aws_search_users_results | filter | Filter user archive pages results. |
aws_js_seamless_selectors | filter | Selectors for search form js replacement. |
aws_js_seamless_form_id | filter | Form that will be used for JS seamless integration. |
aws_js_seamless_searchbox_markup | filter | Filter seamless integrations default form markup. |
aws_search_start | action | Fires each time when performing the search. |
aws_cache_clear | action | Fires each time when the plugin cache was cleared. |
aws_form_changed | action | Fires after the search form instance was created/copied/deleted. |
aws_filters_changed | action | Fires after the search form filter was created/copied/deleted. |
aws_settings_saved | action | Fires after the plugin settings were saved. |
aws_create_index_table | action | Fires when the plugin index table is created. |
aws_index_complete | action | Fires once the index process is complete. |
apply_filters( 'aws_meta_keys', (array) $meta_keys );
Admin hook. Final array of meta fields for the settings page. Contains meta fields that can be enabled for search.
By default inner meta fields ( field names starts with _
) are not showing inside the plugin settings page. Let's show _kad_tab_
inside the settings page and make it possible to enable search for it.
add_filter( 'aws_meta_keys', 'my_aws_meta_keys' ); function my_aws_meta_keys( $meta_keys ) { $meta_slug = '_kad_tab_'; $meta_name = 'meta_' . strtolower( $meta_slug ); $meta_keys[$meta_name] = $meta_slug; return $meta_keys; }
apply_filters( 'aws_meta_keys_include', (array) $include_meta );
Admin hook. Add to the array of meta fields additional fields that are excluded by default.
By default inner meta fields ( field names starts with _
) are not showing inside the plugin settings page. Let's show _product_subtitle
and _custom_product_features
inside the settings page and make it possible to enable search inside them.
add_filter( 'aws_meta_keys_include', 'my_aws_meta_keys_include' ); function my_aws_meta_keys_include( $include_meta ) { $include_meta[] = '_product_subtitle'; $include_meta[] = '_custom_product_features '; return $include_meta; }
apply_filters( 'aws_meta_keys_unfiltered', (array) $meta );
Admin hook. Array of meta fields for the settings page before filtering. Include all inner meta fields ( that name starts with _
).
Remove meta field aliases
from the plugin settings page.
add_filter( 'aws_meta_keys_unfiltered', 'my_aws_meta_keys_unfiltered' ); function my_aws_meta_keys_unfiltered( $meta ) { if ( is_array( $meta ) && !empty( $meta ) ) { foreach ( $meta as $arr_key => $field ) { if ( isset( $field->meta_key ) && $field->meta_key === 'aliases' ) { unset( $meta[$arr_key] ); } } } return $meta; }
apply_filters( 'aws_admin_page_options', (array) $options );
Admin area hook. Filter options array for the plugin settings page.
Lets add a new option called show_on_mobile
. When disable it must hide search form on mobile devices. Note that in this example we only add show_on_mobile
option inside the plugin settings page and not create any logic that must hide the search form depending on this option value.
add_filter( 'aws_admin_page_options', 'my_aws_admin_page_options' ); function my_aws_admin_page_options( $options ) { $options['general'][] = array( "name" => __( "Show on mobile", "advanced-woo-search" ), "desc" => __( "Show or not search form on mobile devices.", "advanced-woo-search" ), "id" => "show_on_mobile", "value" => 'true', "type" => "select", 'choices' => array( 'true' => __( 'Show', 'advanced-woo-search' ), 'false' => __( 'Hide', 'advanced-woo-search' ), ) ); return $options; }
apply_filters( 'aws_before_strip_shortcodes', (string) $str );
Index table hook. Filter product content strings before stripping shortcodes and add content to the plugin index table.
We have shortcode [my_shortcode]
and want to replace it with some custom value before it will be stripped.
add_filter( 'aws_before_strip_shortcodes', 'my_aws_before_strip_shortcodes' ); function my_aws_before_strip_shortcodes( $str ) { $str = str_replace( '[my_shortcode]', 'Custom shortcode contetn', $str ); return $str; }
apply_filters( 'aws_special_chars', (array) $chars );
Special characters to remove from the product strings.
For example, we have products with strings 21.R31
and 21R31
. Plugin default behaviour is to strip .
character from the search string. So no matter will the user search for 21.R31
or21R31
- the search results will be the same. But if we want to show different results for these two queries we need to disable the stripping of the dot .
characters.
add_filter( 'aws_special_chars', 'my_aws_special_chars' ); function my_aws_special_chars( $chars ) { unset( $chars[array_search( '.',$chars )] ); return $chars; }
apply_filters( 'aws_diacritic_chars', (array) $chars );
Filters array of diacritic chars. Array contains pare diacritic char => character to replace with.
By default if inside out product content we have character ž
it will be replaced with z
letter. Lets change this and replace character ž
with ze
.
add_filter( 'aws_diacritic_chars', 'my_aws_diacritic_chars' ); function my_aws_diacritic_chars( $chars ) { $chars['ž'] = 'ze'; return $chars; }
apply_filters( 'aws_normalize_string', (string) $string );
Filters products normalized ( without special characters, html tags and shortcodes ) strings.
For example we want to search for product SKU numbers. Sometimes these SKUs contain prefix VOE
and sometimes. Like VOE123456
or 654321
. But we need exact SKU search without looking at this prefix. The best solution here - just remove VOE
from the product SKU.
add_filter( 'aws_normalize_string', 'my_aws_normalize_string' ); function my_aws_normalize_string( $string ) { $pattern = '/(VOE)+/i'; $string = preg_replace( $pattern, '', $string ); return $string; }
Another example - we have two products with strings 21.R31
and 21R31
. By default plugin removes special characters like .
from the string. So 21.R31
becomes 21R31
. But what to do if we enable the exact match search ( available with PRO version ) and search for R31
? Default results - empty. But we can replace .
with the empty space character, so during the index process 21.R31
becomes 21 R31
and exact match search for R31
will show proper search results.
add_filter( 'aws_normalize_string', 'my_aws_normalize_string' ); function my_aws_normalize_string( $string ) { $string = str_replace( '.', ' ', $string ); return $string; }
apply_filters( 'aws_synonyms_option_array', (array) $synonyms );
Filters synonyms array before adding them to the index table where needed. Contains the same synonyms groups that were specified inside the plugin settings page.
Lets add new synonyms group box, package, pac
. Instead of adding it from the plugin settings page we will use a current filter.
add_filter( 'aws_synonyms_option_array', 'my_aws_synonyms_option_array' ); function my_aws_synonyms_option_array( $synonyms ) { $synonyms[] = 'box, package, pac'; return $synonyms; }
apply_filters( 'aws_front_filters', (array) $filters, (int) $form_id );
Array of search form filters before output. Contains filter created via plugin settings page.
Remove the second form filter id if the search form is inside product_cat
archive page.
add_filter( 'aws_front_filters', 'my_aws_front_filters', 10, 2 ); function my_aws_front_filters( $filters, $form_id ) { if ( is_tax( 'product_cat' ) ) { unset( $filters['filters'][1] ); } return $filters; }
apply_filters( 'aws_front_data_parameters', (array) $params, (int) $form_id );
Array of search form settings before generate the form.
Change SKU
word for product search results to ISBN
.
add_filter( 'aws_front_data_parameters', 'my_aws_front_data_parameters', 10, 2 ); function my_aws_front_data_parameters( $params, $form_id ) { $params['data-sku'] = 'ISBN'; return $params; }
Disable AJAX search for mobile devices.
add_filter( 'aws_front_data_parameters', 'my_aws_front_data_parameters', 10, 2 ); function my_aws_front_data_parameters( $params ) { if ( wp_is_mobile() ) { $params['data-show-page'] = 'ajax_off'; } return $params; }
apply_filters( 'aws_searchbox_markup', (string) $markup, (array) $params );
Array of search form settings before generating the form.
Add description Start typing your search words here
near search form.
add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup', 10, 2 ); function my_aws_searchbox_markup( $markup, $params ) { $content = 'Start typing your search words here'; $form = '<form class="aws-search-form"'; $markup = str_replace( $form, $content . $form, $markup ); return $markup; }
Change search form default magnifier svg icon.
add_filter( 'aws_searchbox_markup', 'my_aws_searchbox_markup', 10, 2 ); function my_aws_searchbox_markup( $markup, $params ) { $pattern = '/<svg[Ss]*?</svg>/i'; $new_icon = '<style>.aws-container .aws-search-form .aws-search-btn_icon{height: 18px;}</style></style><svg aria-hidden="true" data-prefix="fas" data-icon="search" class="svg-inline--fa fa-search fa-w-16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"> <path fill="currentColor" d="M505 442.7L405.3 343c-4.5-4.5-10.6-7-17-7H372c27.6-35.3 44-79.7 44-128C416 93.1 322.9 0 208 0S0 93.1 0 208s93.1 208 208 208c48.3 0 92.7-16.4 128-44v16.3c0 6.4 2.5 12.5 7 17l99.7 99.7c9.4 9.4 24.6 9.4 33.9 0l28.3-28.3c9.4-9.4 9.4-24.6.1-34zM208 336c-70.7 0-128-57.2-128-128 0-70.7 57.2-128 128-128 70.7 0 128 57.2 128 128 0 70.7-57.2 128-128 128z"></path> </svg>'; $markup = preg_replace( $pattern, $new_icon, $markup ); return $markup; }
apply_filters( 'aws_search_page_filters', (array) $filters );
Active product filter of current search results page. Contains such values like on_sale
, price_min
, price_max
, rating
, etc.
If price filters are not active that show only products in price range 0 - 100.
add_filter( 'aws_search_page_filters', 'my_aws_search_page_filters' ); function my_aws_search_page_filters( $filters ) { if ( ! isset( $filters['price_max'] ) && ! isset( $filters['price_min'] ) ) { $filters['price_max'] = '100'; $filters['price_min'] = '0'; } return $filters; }
apply_filters( 'aws_products_search_page_filtered', (array) $products );
Product on search results page after filters apply.
For the search results page lets remove products with ID 7966
.
add_filter( 'aws_products_search_page_filtered', 'my_aws_products_search_page_filtered' ); function my_aws_products_search_page_filtered( $products ) { if ( $products && is_array( $products ) ) { foreach( $products as $key => $product ) { if ( $product['id'] === 7966 ) { unset( $products[$key] ); } } } return $products; }
apply_filters( 'aws_products_order_by', (string) $order_by, (object) $query );
Filter order by value for search results page.
Change orderby
value to price
if page default orderby
value set to meta_value_num
.
add_filter( 'aws_products_order_by', 'my_aws_products_order_by', 10, 2 ); function my_aws_products_order_by( $order_by, $query ) { if ( $query->query_vars['orderby'] === 'meta_value_num' ) { $order_by = 'price'; } return $order_by; }
apply_filters( 'aws_products_order', (array) $products, (string) $order_by );
Product on search results page after the ordering.
For the search results page lets remove products with ID 7966
.
add_filter( 'aws_products_order', 'my_aws_products_order', 10, 2 ); function my_aws_products_order( $products, $order_by ) { if ( $products && is_array( $products ) ) { foreach( $products as $key => $product ) { if ( $product['id'] === 7966 ) { unset( $products[$key] ); } } } return $products; }
apply_filters( 'aws_index_posts_per_page', (int) $num );
Index table hook. Number of products to be indexed per one request. Largest number can speed-up the index process but can lead to time-out error.
Increase the number of products to be indexed per one request to 50
.
add_filter( 'aws_index_posts_per_page', 'my_aws_index_posts_per_page' ); function my_aws_index_posts_per_page( $num ) { return 50; }
apply_filters( 'aws_index_cron_runner_time_limit', (int) $num );
Index table hook. Maximum execution time for the cron index script.
Increase maximum execution time to 1000
.
add_filter( 'aws_index_cron_runner_time_limit', 'my_aws_index_cron_runner_time_limit' ); function my_aws_index_cron_runner_time_limit( $num ) { return 1000; }
apply_filters( 'aws_index_max_cron_attemps', (int) $num );
Index table hook. Max number of the cron index script repeats before failing.
Decrease number of cron job attempts to 5
.
add_filter( 'aws_index_max_cron_attemps', 'my_aws_index_max_cron_attemps' ); function my_aws_index_max_cron_attemps( $num ) { return 5; }
apply_filters( 'aws_index_product_ids', (array) $ids );
Filter the array of product IDs that will be indexed.
Remove from the plugin index table products with IDs 8056
and 8036
.
add_filter( 'aws_index_product_ids', 'my_aws_index_product_ids' ); function my_aws_index_product_ids( $ids ) { unset( $ids[array_search( 8056, $ids )] ); unset( $ids[array_search( 8036, $ids )] ); return $ids; }
apply_filters( 'aws_index_apply_filters', (bool) $apply );
Apply or not the_content
filter to product content before the index. Some additional content can be added via this hook. Enabling can lead to a heavy increase of the plugin index table.
Enably apply of the_content
filter for product content.
add_filter( 'aws_index_apply_filters', 'my_aws_index_apply_filters' ); function my_aws_index_apply_filters( $apply ) { return true; }
apply_filters( 'aws_indexed_title', (string) $title, (int) $product_id, (object) $product );
Index table hook. Filters product title before it will be indexed.
For example we have a grouped product that has several child products inside. We want to display inside search results this main grouped product when the user searches for one of the child products title
or SKU
.
add_filter( 'aws_indexed_title', 'my_aws_indexed_title', 10, 3 ); function my_aws_indexed_title( $title, $id, $product ) { if ( $product->is_type( 'grouped' ) ) { $children = $product->get_children(); if ( sizeof( $children ) > 0 ) { foreach ( $children as $child_id ) { $child_product = wc_get_product( $child_id ); $title .= ' ' . get_the_title( $child_id ); $title .= ' ' . $child_product->get_sku(); } } } return $title; }
Some products contain stock_number
custom fields. We want to show the corresponding products when users search for this custom field value. This can be achieved by adding stock_number
field value to product title for indexing.
add_filter( 'aws_indexed_title', 'my_aws_indexed_title', 10, 3 ); function my_aws_indexed_title( $title, $id, $product ) { $stock_number = get_post_meta( $id, 'stock_number', true ); if ( $stock_number ) { $title .= ' ' . $stock_number; } return $title; }
apply_filters( 'aws_indexed_content', (string) $content, (int) $product_id, (object) $product );
Index table hook. Filters product content before it will be indexed.
We have the product with custom fields custom_field
. We want to show the corresponding products when users search for this field value. This can be achieved by adding custom_field
field value to the product content during the index process.
add_filter( 'aws_indexed_content', 'my_aws_indexed_content', 10, 3 ); function my_aws_indexed_content( $content, $id, $product ) { $custom_field = get_post_meta( $id, 'custom_field', true ); if ( $custom_field ) { $content .= ' ' . $custom_field; } return $content; }
apply_filters( 'aws_indexed_excerpt', (string) $excerpt, (int) $product_id, (object) $product );
Index table hook. Filters product short description before it will be indexed.
If we are not using product short description as a search source then it is possible to fully remove it from the plugin index table. This will decrease index table size and can speed-up search.
add_filter('aws_indexed_excerpt', 'my_aws_indexed_excerpt', 10, 3 ); function my_aws_indexed_excerpt( $excerpt, $id, $product ) { return ''; }
We have the product with custom fields product_vin_number
that contains VIN code of the current product. We want to enable product search via this VIN number. This can be achieved by adding product_vin_number
field value to the product short description during the index process.
add_filter( 'aws_indexed_excerpt', 'my_aws_indexed_excerpt', 10, 3 ); function my_aws_indexed_excerpt( $excerpt, $id, $product ) { $product_vin_number = get_post_meta( $id, 'product_vin_number', true ); if ( $product_vin_number ) { $excerpt .= ' ' . $product_vin_number; } return $excerpt; }
apply_filters( 'aws_indexed_custom_fields', (array) $custom_fields, (int) $product_id, (object) $product );
Index table hook. Filters product custom fields before they will be indexed.
If we are not using search via product custom fields then it is possible to fully remove them from the plugin index table. This action will decrease index table size and can speed-up search.
add_filter( 'aws_indexed_custom_fields', 'my_aws_indexed_custom_fields', 10, 3 ); function my_aws_indexed_custom_fields( $custom_fields, $product_id, $product ) { return ''; }
apply_filters( 'aws_indexed_data', (array) $data, (int) $product_id );
Index table hook. Filters product data before adding to the index table.
If we are not using search via product custom fields, taxonomies and attributes then it is possible not to index these data. This action will decrease index table size and can speed-up search.
add_filter( 'aws_indexed_data', 'my_aws_indexed_data', 10, 2 ); function my_aws_indexed_data( $data, $product_id ) { foreach ( $data['terms'] as $source => $all_terms ) { if ( strpos( $source, 'meta_' ) === 0 || strpos( $source, 'tax_' ) === 0 || strpos( $source, 'attr_' ) === 0 ) { unset( $data['terms'][$source] ); } } return $data; }
Plugin gives the option to search for product variations and display them inside search results. If you don't need such a feature then it is possible to remove product variations data from the index table.
add_filter( 'aws_indexed_data', 'my_aws_indexed_data', 10, 2 ); function my_aws_indexed_data( $data, $product_id ) { if ( $data['type'] === 'child' ) { return false; } return $data; }
apply_filters( 'aws_create_index_table_sql', (string) $sql );
Index table hook. Filters SQL query that will be used to create index table.
Lets modify SQL query and change term
column type from VARCHAR(50)
to TEXT
. New column type can store a larger number of characters for product terms.
When a code snippet is added it is needed to re-index the plugin table in order to affect the changes.
add_filter( 'aws_create_index_table_sql', 'aws_create_index_table_sql' ); function aws_create_index_table_sql( $sql ) { $sql = str_replace( 'term VARCHAR(50) NOT NULL DEFAULT 0', 'term TEXT NOT NULL', $sql ); return $sql; }
apply_filters( 'aws_extracted_string', (string) $string, (string) $source );
Index table hook. Filters extracted from the product data strings before index.
Lets remove all digits from product title
and content
sources. This can be useful if you need to ignore digits from the user query and search only for words.
add_filter( 'aws_extracted_string', 'my_aws_extracted_string', 10, 2 ); function my_aws_extracted_string( $string, $source ) { if ( $source === 'title' || $source === 'content' ) { $string = preg_replace( '/[0-9]+/', '', $string ); } return $string; }
apply_filters( 'aws_extracted_terms', (array) $str_array, (string) $source );
Index table hook. Filters extracted from the product data strings before index.
For example we enable search by product SKUs. And we notice that often users type inside the search field 0o
instead 00
. So it is some sort of typo. It is possible to show correct search results even with such mistakes by adding inside the plugin index table both variations of terms: with 00
and with 0o
.
add_filter( 'aws_extracted_terms', 'my_aws_extracted_terms', 10, 2 ); function my_aws_extracted_terms( $str_array, $source ) { $new_terms = array(); if ( $str_array && is_array( $str_array ) && ! empty( $str_array ) ) { foreach ( $str_array as $str_term => $str_num ) { if( strpos( $str_term, '00' ) !== false ) { $new_terms[] = str_replace( '00', 'o0', $str_term ); } } } if ( $new_terms ) { foreach( $new_terms as $new_term ) { $str_array[$new_term] = 1; } } return $str_array; }
For example we have a product with the term 5555g55
. And we want to show it for both search queries 5555g55
or 555555
. So we need to separate words and digits for all product terms.
add_filter( 'aws_extracted_terms', 'my_aws_extracted_terms', 10, 2 ); function my_aws_extracted_terms( $str_array, $source ) { $new_terms = array(); if ( $str_array && is_array( $str_array ) && ! empty( $str_array ) ) { foreach ( $str_array as $str_term => $str_num ) { if ( preg_match( "/b[d]+([^dW]+)[d]+b/i", $str_term, $matches ) ) { $new_terms[] = str_replace( $matches[1], '', $str_term ); } } } if ( $new_terms ) { foreach( $new_terms as $new_term ) { $str_array[$new_term] = 1; } } return $str_array; }
apply_filters( 'aws_page_results', (int) $number);
Total number of search results for search results page.
Set the total number of items for the search results page to 999
.
add_filter( 'aws_page_results', 'my_aws_page_results' ); function my_aws_page_results( $num ) { return 999; }
apply_filters( 'aws_posts_per_page', (int) $number);
Number of search results per page for search results page.
Set the number of search results items per page to 12
.
add_filter( 'aws_posts_per_page', 'my_aws_posts_per_page' ); function my_aws_posts_per_page( $num ) { return 12; }
apply_filters( 'aws_searchpage_enabled', (bool) $is_enabled, (object) $query );
Display or not plugin results for current search results page.
Current filter gives option to disable plugin search results when needed. For example, let's disable plugin results when we have aws_off
GET parameter for current search results page.
add_filter( 'aws_searchpage_enabled', 'my_aws_searchpage_enabled', 10, 2 ); function my_aws_searchpage_enabled( $is_enabled, $query ) { if ( isset( $_GET['aws_off'] ) && $_GET['aws_off'] === 'true' ) { return false; } return $is_enabled; }
apply_filters( 'aws_search_page_results', (array) $results, (object) $query, (array) $data );
Array of results for search results page.
It is possible to add a custom product inside the current search results page. Lets add Xeno Superstar Shoes
as the first search result.
add_filter( 'aws_search_page_results', 'my_aws_search_page_results', 10, 3 ); function my_aws_search_page_results( $results, $query, $data ) { $post = new stdClass(); $post->ID = '12283 '; $post->site_id = get_current_blog_id(); $post->post_name = 'Xeno Superstar Shoes'; $post->post_title = 'Xeno Superstar Shoes'; $post->permalink = '#'; $post->post_type = "product"; $post->post_author = "1"; $post->post_status = "publish"; $post->post_parent = 0; $post->post_content = ""; $post->menu_order = 0; $post->post_date = "2000-01-01 12:00:00"; $post->post_date_gmt = "2000-01-01 12:00:00"; $post->post_modified = "2000-01-01 12:00:00"; $post->post_modified_gmt = "2000-01-01 12:00:00"; $post->awssearch = true; $results = array( $post ) + $results; return $results; }
apply_filters( 'aws_search_page_query', (string) $search_query, (object) $query );
Filter search query string for search results page.
Lets add additional search word woo
for all search queries for the search results page. This means that additionally to user search query results will be shown all products that have word woo
inside its content.
add_filter( 'aws_search_page_query', 'my_aws_search_page_query', 10, 2 ); function my_aws_search_page_query( $search_query, $query ) { return $search_query . ' woo'; }
apply_filters( 'aws_search_results_products_ids', (array) $posts_ids, (string) $s );
Filter the array of product IDs that will be displayed as search results.
We want to show some products at the beginning of the search results list. And what exactly products to show depends on search terms. For example, if a search query contains search term term1
then products with ID 12
and 13
must be displayed first. And for term term2
it must be products 14
and 15
.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids', 10, 2 ); function my_aws_search_results_products_ids( $posts_ids, $s ) { $terms = array( 'term1' => array( 12, 13 ), 'term2' => array( 14, 15 ) ); foreach( $terms as $term => $ids ) { if ( strpos( $s, $term ) !== false ) { foreach( $ids as $id ) { if ( $key = array_search( $id, $posts_ids ) ) { unset( $posts_ids[$key] ); } } foreach( $ids as $id ) { array_unshift( $posts_ids, $id ); } } } return $posts_ids; }
It is possible to fully hide all products from the search results list. Useful if, for example, you want to display only product categories or tags as search results.
add_filter( 'aws_search_results_products_ids', 'my_aws_search_results_products_ids', 10, 2 ); function my_aws_search_results_products_ids( $posts_ids, $s ) { return array(); }
apply_filters( 'aws_search_results_products', (array) $products, (string) $s );
Filter the array of products search results.
Lets order products inside search results by stock status: first display in stock
products and out of stock
products in the end.
add_filter( 'aws_search_results_products', 'aws_search_results_products', 10, 2 ); function aws_search_results_products( $products, $s ) { usort($products, function ($item1, $item2) { $product1 = wc_get_product( $item1['id'] ); $product2 = wc_get_product( $item2['id'] ); if ( 'outofstock' !== $product1->get_stock_status() ) { return -1; } if ( 'outofstock' !== $product2->get_stock_status() ) { return 1; } return 0; }); return $products; }
Hide products with zero price from the list of search results.
add_filter( 'aws_search_results_products', 'my_aws_search_results_products', 10, 2 ); function my_aws_search_results_products( $products, $s ) { $new_array = array(); foreach ( $products as $product ) { if ( ! isset( $product['f_price'] ) || intval( $product['f_price'] ) !== 0 ) { $new_array[] = $product; } } return $new_array; }
apply_filters( 'aws_search_results_tax_archives', (array) $taxonomies, (string) $s );
Array of enabled for search taxonomies archive pages.
For example we want to disable search for product categories archive pages if the search form is placed inside product_cat
taxonomy archive page.
add_filter( 'aws_search_results_tax_archives', 'my_aws_search_results_tax_archives', 10, 2 ); function my_aws_search_results_tax_archives( $taxonomies, $s ) { if ( is_tax( 'product_cat' ) ) { foreach( $taxonomies as $tax_key => $tax_slug ) { if ( $tax_slug === 'product_cat' ) { unset( $taxonomies[$tax_key] ); } } } return $taxonomies; }
apply_filters( 'aws_search_results_all', (array) $results, (string) $s );
Filter all search results before displaying them inside the search results list.
We have users with role dc_vendor
and want to display them inside the search results block at the first position.
add_filter( 'aws_search_results_all', 'my_aws_search_results_all', 10, 2 ); function my_aws_search_results_all( $results, $s ) { $vendors = get_users( array( 'role' => 'dc_vendor', 'orderby' => 'user_nicename', 'order' => 'ASC' ) ); if ( $vendors ) { foreach( $vendors as $vendor ) { $name = $vendor->data->user_nicename; $new_result = array( 'name' => $name, 'link' => '/store/' . $name, ); $results['tax']['user'][] = $new_result; } } return $results; }
apply_filters( 'aws_search_terms', (array) $terms );
Filters array of search terms before generating SQL query.
For example we have a product with the term 5555g55
. And we want to show it for both search queries 5555g55
or 555555
. So we need to separate words and digits for all product terms.
add_filter( 'aws_search_terms', 'my_aws_search_terms' ); function my_aws_search_terms( $terms ) { $new_terms = array(); if ( $terms ) { foreach( $terms as $term ) { if ( preg_match( "/[d]+/i", $term ) ) { if ( preg_match( "/b([^dW]+)([d]+)b/i", $term, $matches ) || preg_match( "/b([d]+)([^dW]+)b/i", $term, $matches ) ) { if ( strlen( $matches[1] ) > 1 ) { $new_terms[] = $matches[1]; } if ( strlen( $matches[2] ) > 1 ) { $new_terms[] = $matches[2]; } } } } if ( ! empty( $new_terms ) ) { $terms = array_merge( $terms, $new_terms ); } } return $terms; }
apply_filters( 'aws_tax_filter_include_childs', (bool) $include, (int) $form_id, (int) $filter_id );
When using exclude/include filters it is possible to filter search results by specific taxonomy terms. By default filter will apply and to all child terms too. But this behavior can be changed.
Disable applying filters for the child terms.
add_filter( 'aws_tax_filter_include_childs', 'my_aws_tax_filter_include_childs' ); function my_aws_tax_filter_include_childs( $include, $form_id, $filter_id ) { return false; }
apply_filters( 'aws_exclude_products', (array) $products, (int) $form_id, (int) $filter_id );
Filter the array of product IDs that must be excluded from the search results.
Lets hide products with IDs 75
, 78
and 83
from the search results for non logged-in users.
add_filter( 'aws_exclude_products', 'my_aws_exclude_products', 10, 3 ); function my_aws_exclude_products( $products, $form_id, $filter_id ) { if ( ! is_user_logged_in() ) { $products[] = 75; $products[] = 78; $products[] = 83; } return $products; }
apply_filters( 'aws_search_current_lang', (string) $lang, (int) $form_id, (int) $filter_id );
On what language to display search results. Contains active language code of the current page.
By default the plugin will show search results on the language of the current page. Lets change this and just show all available products translations.
add_filter( 'aws_search_current_lang', 'my_aws_search_current_lang', 10, 3 ); function my_aws_search_current_lang( $lang, $form_id, $filter_id ) { return ''; }
apply_filters( 'aws_search_query_array', (array) $query, (int) $form_id, (int) $filter_id );
Filter search query parameters.
Add more relevance score form terms that were found inside product tags.
add_filter( 'aws_search_query_array', 'my_aws_search_query_array' ); function my_aws_search_query_array( $query ) { $relevance = "( case when ( term_source = 'tag' ) then 250 else 0 end ) +"; $query['relevance'] = preg_replace( '/(SUM([sS]*?([sS]*?case[sS]*?end[sS]*?)[sS]*?+/i', '$0' . $relevance, $query['relevance'] ); return $query; }
Don't show products with a publish date earlier than 2014-01-01
. In this example we also need to use aws_search_query_string
filter.
add_filter( 'aws_search_query_array', 'my_aws_search_query_array' ); function my_aws_search_query_array( $query ) { global $wpdb; $date = '2014-01-01 00:00:00'; $table = $wpdb->prefix . AWS_INDEX_TABLE_NAME; $query['select'] = str_replace( 'ID', $table . '.ID', $query['select'] ); $query['search'] .= "AND {$table}.ID = {$wpdb->posts}.ID AND {$wpdb->posts}.post_date > STR_TO_DATE('{$date}', '%Y-%m-%d %H:%i:%s')"; return $query; } add_filter( 'aws_search_query_string', 'my_aws_search_query_string' ); function my_aws_search_query_string( $sql ) { global $wpdb; $sql = str_replace( 'FROM', 'FROM ' . $wpdb->posts . ',', $sql ); return $sql; }
apply_filters( 'aws_search_query_string', (string) $sql, (int) $form_id, (int) $filter_id );
Filter search query SQL string.
Limit search results to only 5 products.
add_filter( 'aws_search_query_string', 'my_aws_search_query_string' ); function my_aws_search_query_string( $sql ) { $sql = str_replace( 'LIMIT 0, 10', 'LIMIT 0, 5', $sql ); return $sql; }
apply_filters( 'aws_title_search_result', (string) $title, (int) $product_id, (object) $product);
Product title inside search results list.
For example we have a product custom field YOUR_FIELD
and want to display its value inside the product title for search results.
add_filter( 'aws_title_search_result', 'my_aws_title_search_result', 10, 3 ); function my_aws_title_search_result( $title, $product_id, $product ) { $field = get_post_meta( $product_id, 'YOUR_FIELD', true ); if ( $field ) { $title = $title . '<br>' . $field; } return $title; }
apply_filters( 'aws_excerpt_search_result', (string) $excerpt, (int) $product_id, (object) $product);
Filter product description inside search results list.
We have Advanced Custom Fields ( ACF ) plugin installed and with its help created acf_test_field
product field. It is possible to display the value from these fields inside the product description when displaying plugin search results.
add_filter( 'aws_excerpt_search_result', 'my_aws_excerpt_search_result', 10, 3 ); function my_aws_excerpt_search_result( $excerpt, $product_id, $product ) { $field_name = 'acf_test_field'; if ( function_exists( 'get_field' ) ) { $field = get_field( $field_name, $product_id ); if ( $field ) { $excerpt .= '<br>' . $field; } } return $excerpt; }
apply_filters( 'aws_search_pre_filter_products', (array) $products, (array) $data);
Filter products array before the output.
Hide products price for non logged-in users.
add_filter( 'aws_search_pre_filter_products', 'my_aws_search_pre_filter_products' ); function my_aws_search_pre_filter_products( $products ) { if ( $products && ! empty( $products ) ) { foreach ( $products as $key => $product ) { if ( $product['price'] && ! is_user_logged_in() ) { $products[$key]['price'] = ''; } } } return $products; }
Change Add to cart
button success text and View cart
link.
add_filter( 'aws_search_pre_filter_products', 'my_aws_search_pre_filter_products' ); function my_aws_search_pre_filter_products( $products ) { if ( $products && ! empty( $products ) ) { foreach ( $products as $key => $product ) { if ( $product['add_to_cart'] ) { $products[$key]['add_to_cart']['i18n_view_cart'] = esc_attr__( 'Added', 'advanced-woo-search' ); $products[$key]['add_to_cart']['cart_url'] = ''; } } } return $products; }
apply_filters( 'aws_image_size', (string) $image_size );
Size of product images inside search results block.
Change products image size to full
.
add_filter( 'aws_image_size', 'my_aws_image_size' ); function my_aws_image_size( $image_size ) { return 'full'; }
apply_filters( 'aws_highlight_tag', (string) $tag );
Tag to use for highlighting search words inside the content.
strong
.Change HTML highlighting tag to i
.
add_filter( 'aws_highlight_tag', 'my_aws_highlight_tag' ); function my_aws_highlight_tag( $tag ) { return 'i'; }
apply_filters( 'aws_tax_search_data', (array) $data, (array) $taxonomy );
Filters the array of taxonomies search data.
Lets change search logic for taxonomies archive pages to AND.
add_filter( 'aws_tax_search_data', 'my_aws_tax_search_data', 10, 2 ); function my_aws_tax_search_data( $data, $taxonomy ) { $data['search_logic'] = 'and'; return $data; }
apply_filters( 'aws_search_terms_number', (int) $num );
Maximal number of taxonomies archive pages inside search results.
Changing the maximal number of taxonomies archive pages results to 5
.
add_filter( 'aws_search_terms_number', 'my_aws_search_terms_number' ); function my_aws_search_terms_number( $num ) { return 5; }
apply_filters( 'aws_terms_search_query', (string) $sql, (string) $taxonomy, (string) $search_query );
Filter taxonomies archive pages SQL query string.
By default the plugin will not show inside search results block taxonomy archive pages that have no products inside. But this can be changed with the following code snippet.
add_filter( 'aws_terms_search_query', 'my_aws_terms_search_query', 10, 3 ); function my_aws_terms_search_query( $sql, $taxonomy, $search_query ) { $sql = str_replace( 'AND count > 0', '', $sql ); return $sql; }
apply_filters( 'aws_search_tax_results', (array) $result_array, (string) $taxonomy, (string) $search_query );
Filter taxonomies archive pages results.
For example we have taxonomy pwb-brand
and each of this taxonomy term has a unique image that is stored inside the pwb_brand_image
meta field. We want to show this image for each taxonomy term inside the search results block.
add_filter( 'aws_search_tax_results', 'my_aws_search_tax_results', 10, 3 ); function my_aws_search_tax_results( $result_array, $taxonomy, $search_query ) { if ( isset( $result_array['pwb-brand'] ) && ! empty( $result_array['pwb-brand'] ) ) { foreach ( $result_array['pwb-brand'] as $key => $brand ) { $thumb_id = get_term_meta( $brand['id'], 'pwb_brand_image', 1 ); $thumb_src = wp_get_attachment_image_src( $thumb_id, 'thumbnail' ); if ( $thumb_src ) { $result_array['pwb-brand'][$key]['image'] = $thumb_src[0]; } } } return $result_array; }
apply_filters( 'aws_search_terms_description', (bool) $enable );
Search or not inside the taxonomy terms description. By default plugin search only inside terms names.
false
.Enable searching inside taxonomies terms description.
add_filter( 'aws_search_terms_description', 'my_aws_search_terms_description' ); function my_aws_search_terms_description( $enable ) { return true; }
apply_filters( 'aws_search_tax_exclude', (array) $exclude, (array) $taxonomy, (string) $search_query );
Exclude certain taxonomies terms archive pages from the search results.
Exclude taxonomy term with ID 19
from the search.
add_filter( 'aws_search_tax_exclude', 'my_aws_search_tax_exclude', 10, 3 ); function my_aws_search_tax_exclude( $exclude, $taxonomy, $search_query ) { $exclude[] = 19; return $exclude; }
apply_filters( 'aws_terms_exclude_' . $taxonomy, (array) $exclude );
Exclude certain terms archive pages from the search results.
Exclude from the search term with ID 125
that belongs to product_cat
taxonomy.
add_filter( 'aws_terms_exclude_product_cat', 'my_aws_terms_exclude_product_cat' ); function my_aws_terms_exclude_product_cat( $exclude ) { $exclude[] = 125; return $exclude; }
apply_filters( 'aws_users_search_data', (array) $data, (array) $roles );
Filters the array of users search data.
Lets change search logic for user archive pages to AND.
add_filter( 'aws_users_search_data', 'my_aws_users_search_data', 10, 2 ); function my_aws_users_search_data( $data, $roles ) { $data['search_logic'] = 'and'; return $data; }
apply_filters( 'aws_users_search_args', (array) $args );
Filter users search query arguments that will pass to get_users
WordPress function.
get_users
function.Customize users search results order. By default users ordered by login
. Let's change it and order users results by post_count
.
add_filter( 'aws_users_search_args', 'my_aws_users_search_args' ); function my_aws_users_search_args( $args ) { $args['orderby'] = 'post_count'; return $args; }
apply_filters( 'aws_users_search_query', (string) $sql, (string) $roles, (string) $search_query );
Filter users archive pages SQL query string.
By default users inside search results block order by ID
. We can change this behavior and order users by, for example, display_name
.
add_filter( 'aws_users_search_query', 'my_aws_users_search_query', 10, 3 ); function my_aws_users_search_query( $sql, $roles, $search_query ) { $sql = str_replace( 'GROUP BY ID', 'GROUP BY display_name', $sql ); return $sql; }
apply_filters( 'aws_search_users_results', (array) $results, (string) $roles, (string) $search_query );
Filter user archive pages results.
Customize users archive page URL inside search results block. Change it to '/?user-id=' . $user_id
.
add_filter( 'aws_search_users_results', 'my_aws_search_users_results', 10, 3 ); function my_aws_search_users_results( $results, $roles, $search_query ) { if ( $results ) { foreach( $results as $user_id => $user ) { $results[$user_id][0]['link'] = '/?user-id=' . $user_id; } } return $results; }
apply_filters( 'aws_js_seamless_selectors', (array) $selectors );
Filter css selectors for js seamless integration. Sometimes it is not possible to replace standard search form with the plugin ones with help of any php hooks. In this case there is an option to do replacement with the help on js code. It is only needed to specify css selector of forms that need to be replaced.
We are using the Woodmart
theme and want to replace its search form with the plugin ones. In this case we specify selectors .woodmart-search-form form, form.woodmart-ajax-search
for search forms that will be replaced.
add_filter( 'aws_js_seamless_selectors', 'my_aws_js_seamless_selectors' ); function my_aws_js_seamless_selectors( $selectors ) { $theme = wp_get_theme(); if ( 'Woodmart' === $theme->name ) { $selectors[] = '.woodmart-search-form form, form.woodmart-ajax-search'; } return $selectors; }
apply_filters( 'aws_js_seamless_form_id', (int) $form_id );
Form that will be used for JS seamless integration.
1
Set search form with ID 2
as default form for JS seamless integration.
add_filter( 'aws_js_seamless_form_id', 'my_aws_js_seamless_form_id' ); function my_aws_js_seamless_form_id( $form_id ) { return 2; }
apply_filters( 'aws_js_seamless_searchbox_markup', (string) $form_html, (array) $forms );
Filter form markup that used for seamless integration js event.
We are using some theme where it is possible to replace default search forms with the plugin ones only via js seamless integration hook. And we want to inherit current theme search form styles and also its default search form. To do this we need to change AWS plugin search form markup. We find that theme styles can be inherited if add search-form
class name for plugin search form. Also we need to add some html markup for the search button.
add_filter( 'aws_js_seamless_searchbox_markup', 'my_aws_js_seamless_searchbox_markup' ); function my_aws_js_seamless_searchbox_markup( $markup ) { $button = '<a href="#" class="search-btn">' . lab_get_svg( "images/search.svg" ) . '<span class="sr-only">' . __( "Search", "aurum" ) .'</span></a>'; $markup = str_replace( '</form>', $button . '</form>', $markup ); $markup = str_replace( 'aws-search-form', 'aws-search-form search-form', $markup ); return $markup; }
do_action( 'aws_search_start', (string) $s, (int) $form_id, (int) $filter_id );
Fires each time when performing the search.
add_action( 'aws_search_start', 'my_aws_search_start', 10, 3 ); function my_aws_search_start( $s, $form_id, $filter_id ) { // Do any stuff here }
do_action( 'aws_cache_clear' );
Fires each time when the plugin cache was cleared.
add_action( 'aws_cache_clear', 'my_aws_cache_clear' ); function my_aws_cache_clear() { // Do any stuff here }
do_action( 'aws_form_changed', (array) $settings, (string) $action_type, (int) $instance_id );
Fires after the search form instance was created/copied/deleted.
copy_form
, delete_form
, add_form
add_action( 'aws_form_changed', 'my_aws_form_changed', 10, 3 ); function my_aws_form_changed( $settings, $action_type, $instance_id ) { // Do any stuff here }
do_action( 'aws_filters_changed', (array) $settings, (string) $action_type, (int) $instance_id, (int) $filter_id );
Fires after the search form filter was created/copied/deleted.
add_filter
, copy_filter
, delete_filter
add_action( 'aws_filters_changed', 'my_aws_filters_changed', 10, 4 ); function my_aws_filters_changed( $settings, $action_type, $instance_id, $filter_id ) { // Do any stuff here }
do_action( 'aws_settings_saved', (array) $settings );
Fires after the plugin settings were saved.
add_action( 'aws_settings_saved', 'my_aws_settings_saved' ); function my_aws_settings_saved( $settings ) { // Do any stuff here }
do_action( 'aws_create_index_table' );
Fires when the plugin index table is created.
add_action( 'aws_create_index_table', 'my_aws_create_index_table' ); function my_aws_create_index_table() { // Do any stuff here }
do_action( 'aws_index_complete', (array) $index_meta );
Fires once the index process is complete.
add_action( 'aws_index_complete', 'my_aws_index_complete' ); function my_aws_products_order_by( $index_meta ) { // Do any stuff here }