Skip to content

Conversation

@souleater2023
Copy link

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_chars setting (default: 3 characters)
  • ✅ Added admin interface in Autocomplete settings page
  • ✅ Added validation to ensure value is between 1-10 characters
  • ✅ Updated JavaScript to use minLength parameter
  • ✅ 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

  • Admin interface displays correctly
  • Settings save and validate properly (1-10 range enforced)
  • Frontend autocomplete respects minimum character setting
  • Backward compatibility maintained
  • 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.

- Added algolia_autocomplete_min_chars setting with default of 3 characters
- Added admin interface for configuration (1-10 character range)
- Updated JavaScript to use minLength parameter in autocomplete.js
- Added proper sanitization and validation
- Maintains backward compatibility

This feature reduces API calls to Algolia while maintaining good UX.
Users can configure the minimum number of characters required before
autocomplete search begins via WordPress Admin > Algolia Search > Autocomplete.

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 config
@tw2113
Copy link
Member

tw2113 commented Jul 23, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants