Plugin ArchitectureHooks UsageWp Ajax Wpfts Autocomplete

The wp_ajax_wpfts_autocomplete Hook in WPFTS

The wp_ajax_wpfts_autocomplete hook is a WordPress AJAX hook that processes autocomplete search requests for WPFTS from logged-in users. A version for non-logged-in users, wp_ajax_nopriv_wpfts_autocomplete, exists and functions similarly.

What WPFTS Does When wp_ajax_wpfts_autocomplete is Called:

  1. Retrieving Request Data: The hook retrieves data from the AJAX request, including the user’s search term (s), the widget code (wpfts_wdgt), and other parameters.

  2. Performing the Search: WPFTS performs a search query using the received parameters, employing the WP_Query class. The parameter wpfts_is_force = 1 ensures that the WPFTS search is used, and wpfts_source = 'wpfts_autocomplete_ajax' specifies the request source. If a widget code is provided, its parameters are also considered during the search. Otherwise, default parameters are used, as for the main WordPress query (e.g., post_status = 'publish').

  3. Formatting Results: For each found post, an array is created with the title (label) and link (link).

  4. Applying Smart Excerpts: If the Smart Excerpts option is enabled, the WPFTS_Core::ForceSmartExcerpts() method is called to prepare excerpts for display in the autocomplete results.

  5. Returning Results: The search results are returned in JSON format.

Important Functions Involved in Processing wp_ajax_wpfts_autocomplete:

  • WPFTS_Core::ForceSmartExcerpts()
  • WP_Query

How to Use This in Addon Development:

Addon developers typically don’t need to interact with this hook directly. However, understanding its functionality can be helpful when integrating with WPFTS and creating custom autocomplete features.

Additional Notes:

  • This hook handles requests only from logged-in users. For non-logged-in users, the wp_ajax_nopriv_wpfts_autocomplete hook is used.
  • Search results are limited to the title and link. To display additional information in the autocomplete results, the hook handler code needs to be modified.
  • The hook handler uses the wpfts_json_encode() function for secure JSON encoding of the data.

This hook demonstrates how WPFTS handles autocomplete requests for unauthorized users and can serve as a useful example for addon developers.