wpfts_indexer_event (Action)
The wpfts_indexer_event
hook is triggered each time the WP Fast Total Search indexer runs via the cron schedule. This allows developers to perform custom actions during indexing, such as adding data to the index from third-party sources or performing other operations synchronized with the indexing process.
When to Use
This hook is useful in the following scenarios:
- Adding data to the index from plugins or themes that are not handled by the standard WPFTS indexing rules.
- Performing additional operations during indexing, such as updating related data or sending notifications.
- Monitoring the indexing process and collecting statistics.
Arguments
The wpfts_indexer_event
hook does not accept any arguments.
Return Value
The wpfts_indexer_event
hook should not return any value.
Example
/**
* Adds data from a custom table to the WPFTS index each time the indexer runs.
*/
add_action('wpfts_indexer_event', 'my_custom_indexing_logic');
function my_custom_indexing_logic() {
global $wpdb, $wpfts_core;
// Retrieve data from the custom table.
$data = $wpdb->get_results("SELECT * FROM my_custom_table");
// Add data to the WPFTS index.
foreach ($data as $item) {
$chunks = array(
'custom_title' => $item->title,
'custom_content' => $item->content,
);
$wpfts_core->GetIndex()->reindex(0, $chunks);
// @todo Proper implementation via creating/updating entries in wpftsi_index and adding to wpftsi_rawcache
}
}
Important Notes
- The code executed within this hook’s handler should be optimized for performance, as it will run with each indexer execution, which can be quite frequent.
- Keep in mind that the indexer runs in the background, so you should not output any information to the screen within the hook handler.
- For large amounts of data, it is recommended to use batch processing and optimized SQL queries to add data to the index.
- Critically important: The example above shows an incorrect way to add data to the index. In this case,
$wpfts_core->GetIndex()->reindex(0, $chunks);
is used with a zeroid
, which does not correspond to the plugin’s logic. Developers need to independently create/update entries in thewpftsi_index
table, and also add data towpftsi_rawcache
if necessary. This point should be clearly stated in the documentation, and an example of the correct implementation should be provided.
The wpfts_indexer_event
hook provides developers with the flexibility to integrate their data and logic with the WPFTS indexing process.