post_link Hook in WPFTS

The post_link hook in WordPress, similar to post_type_link, is a filter that allows modification of a post’s permalink before it’s displayed. WPFTS uses this hook to apply Smart Excerpts to post links displayed in search results. Functionally, it’s almost identical to the post_type_link, page_link, and attachment_link hooks.

What WPFTS does when post_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 the search results page (is_search()).
    • The request is not from the admin panel (!is_admin()).
    • The link is displayed within the WordPress loop, or in a Gutenberg block displaying the post title (in_the_loop()).
    • Or, if $wpfts_core->forced_se_query is not equal to false (forced Smart Excerpts call).
  2. Applying Smart Excerpts to the link: If all conditions are met, WPFTS creates a WPFTS_Result_Item object for the post and calls its TitleLink() method, which returns the modified link.

Important functions involved in post_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 post links in search results when using Smart Excerpts. If your addon also modifies post links, ensure your logic doesn’t conflict with WPFTS.

Additional notes:

  • Smart Excerpts are applied to post links only in search results.
  • The post_link filter is called for each post displayed on the search results page.
  • WPFTS adds an extra check for compatibility with the Gutenberg block editor.