The attachment_link hook (filter) in WordPress allows modification of the URL of an attachment link before it’s displayed. WPFTS utilizes this hook to apply Smart Excerpts to attachment links displayed in search results.

What WPFTS Does When attachment_link is Called:

  1. Condition Check: WPFTS checks if the following conditions are met:

    • The plugin is active, and the Smart Excerpts option is enabled (is_smart_excerpts).
    • The request is on a search results page (is_search()).
    • The request is not from the administrative panel (!is_admin()).
    • The link is displayed within the WordPress loop or in a post-title block (in_the_loop()). Additional check for compatibility with the Gutenberg block editor.
    • Or, if $wpfts_core->forced_se_query is not equal to false, meaning Smart Excerpts were forcibly called.
  2. Applying Smart Excerpts to the Link: If all conditions are met, WPFTS creates a WPFTS_Result_Item object for the attachment and calls its TitleLink() method, which returns the modified link. This link may directly point to the attachment file or the attachment page, depending on the plugin settings (is_title_direct_link).

Important Functions Involved in attachment_link Processing:

  • WPFTS_Core::get_option()
  • is_search()
  • is_admin()
  • in_the_loop()
  • WPFTS_Result_Item::TitleLink()
  • WPFTS_Core::ForceSmartExcerpts() (checks the value of $wpfts_core->forced_se_query)

How to Use This in Addon Development:

Addon developers should understand that WPFTS modifies attachment links in search results when using Smart Excerpts. If your addon also modifies attachment links, ensure your logic doesn’t conflict with WPFTS.

Additional Notes:

  • Smart Excerpts are applied to attachment links only in search results.
  • The attachment_link filter is called for each attachment displayed on the page.
  • WPFTS includes an additional check for compatibility with the Gutenberg block editor.