Add configurable minimum characters setting for autocomplete
Summary
This PR adds a configurable minimum characters setting for autocomplete search to reduce API calls to Algolia.
Problem
Currently, autocomplete triggers on every keystroke starting from the first character, which can generate excessive API calls to Algolia, especially for sites with high search traffic.
Solution
This PR adds a configurable "Minimum Characters" setting that allows administrators to specify how many characters users must type before autocomplete search begins.
Changes
- ✅ Added
algolia_autocomplete_min_charssetting (default: 3 characters) - ✅ Added admin interface in Autocomplete settings page
- ✅ Added validation to ensure value is between 1-10 characters
- ✅ Updated JavaScript to use
minLengthparameter - ✅ Added proper sanitization and WordPress filters
- ✅ Maintains full backward compatibility
Benefits
- 🚀 Reduces unnecessary API calls to Algolia
- ⚡ Improves performance for high-traffic sites
- 🎛️ Configurable via WordPress admin interface
- 🔄 Follows existing plugin patterns and coding standards
- 🔒 Includes proper input validation and sanitization
Files Modified
-
includes/class-algolia-settings.php- Added setting and getter method -
includes/indices/class-algolia-index.php- Added to default config -
includes/admin/class-algolia-admin-page-autocomplete.php- Added admin interface -
includes/class-algolia-template-loader.php- Pass setting to frontend -
templates/autocomplete.php- Apply minLength to autocomplete.js
Testing
- [x] Admin interface displays correctly
- [x] Settings save and validate properly (1-10 range enforced)
- [x] Frontend autocomplete respects minimum character setting
- [x] Backward compatibility maintained
- [x] WordPress coding standards followed
Usage
Navigate to WordPress Admin > Algolia Search > Autocomplete and configure the "Minimum Characters" field (default: 3).
This addresses the need to reduce API calls while maintaining good user experience.
Definitely appreciate the PR for this.
While I know you're wanting a minimum of 3 characters before executing, we should leave it at current default 1 for this feature request, as changing would affect all of our users. The filter already being added would definitely be a good use as well.
Also I believe we will want to get the changes for get_default_autocomplete_config() into the other classes that have extended Algolia_Index. Similar to how we did with the debounce functionality.
Looks like there's no minLength for Autocomplete version 1.x+, which we're trying to get moved to offering, but we can make use of the setting somehow and work with the template.