Skip to content

Filter Hooks

Michael Beckwith edited this page Dec 20, 2023 · 20 revisions

The available filters and how to use them to customize the behavior.

Introduction

Filters are a way to alter data during the lifecycle of a request.

Filter Example

The plugin has an exclude mechanism to not display certain post types in the admin UI.

By default, the following post types are disabled:

  • nav_menu_item
  • revision
  • custom_css
  • customize_changeset
  • oembed_cache
  • user_request
  • wp_block
  • wp_global_styles
  • wp_navigation
  • wp_template
  • wp_template_part
  • kr_request_token
  • kr_access_token
  • deprecated_log
  • async-scan-result
  • scanresult

For example, if you have a plugin that is introducing a custom content type, but it is meant to be used internally and you don't want to expose it for indexing.

Note, this filter is for the Autocomplete content type exclusion.

<?php
function my_exclude_custom_post_type( array $exclude ) {
    $exclude[] = 'custom_post_type';

    return $exclude;
}
add_filter( 'algolia_excluded_post_types', 'my_exclude_custom_post_type' );

Your custom_post_type will no longer appear for indexing in the indexing screen. This will also disable tasks being generated for that content type.

For excluding a post type from Instantsearch and "Search Page" settings, use a variation of the following hook and function. With this hook, we've already fetched all the post types that have exclude_from_search set to false, so we need to remove instead of add.

<?php
function my_exclude_searchable_custom_post_type( array $post_types ) {
    if ( array_key_exists( 'custom_post_type', $post_types ) ) {
        unset( $post_types['custom_post_type'] );
    }

    return $post_types;
}
add_filter( 'algolia_searchable_post_types', 'my_exclude_searchable_custom_post_type' );

Filters Reference

Here is the list of all available Filters.

Filter Name Params
algolia_autocomplete_config $config
algolia_autocomplete_input_selector string $input_selector, defaults to "input[name='s']:not('.no-autocomplete')"
algolia_excluded_post_types array $exclude, defaults to array( 'nav_menu_item', revision' )
algolia_excluded_taxonomies array $exclude, defaults to array( 'nav_menu', link_category', 'post_format', 'wp_theme', wp_template_part_area )
algolia_should_index_searchable_post bool $should_index, WP_Post $post
algolia_searchable_post_{$post_type}_shared_attributes array $shared_attributes, WP_Post $post
algolia_searchable_posts_index_settings array $settings
algolia_searchable_post_shared_attributes array $shared_attributes, WP_Post $post
algolia_searchable_post_content string $content
algolia_searchable_posts_index_synonyms array $synonyms
algolia_should_force_settings_update bool $should_force_update (default: false), string $index_id
algolia_should_index_post bool $should_index, WP_Post $post
algolia_should_wait_on_delete_item bool $value, WP_Post $post, array $records
algolia_post_shared_attributes array $shared_attributes, WP_Post $post
algolia_post_{$post_type}_shared_attributes array $shared_attributes, WP_Post $post
algolia_post_content string $content
algolia_post_records array $records, WP_Post $post
algolia_post_{$post_type}_records array $records, WP_Post
algolia_posts_index_settings array $settings, string $post_type
algolia_posts_{$post_type}_index_settings array $settings
algolia_posts_index_synonyms array $synonyms, string $post_type
algolia_posts_{$post_type}_index_synonyms array $synonyms
algolia_should_index_term bool $should_index, WP_Term/object $term
algolia_term_record array $record, WP_Term/object$term
algolia_term_{$taxonomy}_record array $record, WP_Term/object $term
algolia_terms_index_settings array $settings
algolia_terms_{$taxonomy}_index_settings array $settings
algolia_terms_index_synonyms array $synonyms, string $taxonomy
algolia_should_index_user bool $should_index, WP_User $user
algolia_user_record array $record, WP_User $user
algolia_users_index_settings array $settings
algolia_users_index_synonyms array $synonyms
algolia_index_replicas array $replicas, Algolia_Index $index
algolia_{$index_id}_index_replicas array $replicas, Algolia_Index $index
algolia_config array $config
algolia_indexing_batch_size int $batch_size (default: 100)
algolia_{$index_id}_indexing_batch_size int $batch_size
algolia_templates_path string $path (default: 'algolia/')
algolia_template_locations array $locations
algolia_default_template string $template, string $file
algolia_search_params array $params
algolia_search_order_by string $attribute_name
algolia_search_order string $order
algolia_should_override_search_with_instantsearch bool $bool (default: depending on configuration)
algolia_should_override_autocomplete bool $enabled (default: depending on configuration)
algolia_post_images_sizes array $sizes (default: only the 'thumbnail' size)
algolia_get_post_images array $images (default: only the info about the 'thumbnail' size)
algolia_get_synced_indices_ids array $ids
algolia_strip_patterns array $noise_patterns (default: regular expressions to strip comments, cdata, script, style, code, and pre)
algolia_searchable_post_types array $post_types
algolia_native_search_index_id string $index_id
algolia_should_require_search_client bool $bool, (default: true, loads the included Algolia search client library)
algolia_changes_watchers array $watchers (default: Post, Term, User watchers), array $indices
algolia_clear_index_if_existing bool $bool (default: true)
algolia_get_post_object_id string $value Post ID concatenated with the index of the split post record. Separated by dash
algolia_http_client_options array $options Array of HttpClientInterface options
algolia_indices array $indices Array of available indices to load
algolia_load_scripts_in_footer bool $value Filters whether or not to load javascript assets in the footer. (default: true)
algolia_update_records array $records Array of records to be reindexed, $page Current page being reindexed. $value Current ID
algolia_re_index_records array $records Array of records to be re-indexed
algolia_search_highlighting_enable_bundled_styles bool $value Whether or not to output hightlight styles. (default: true)
algolia_search_highlighting_enabled bool $value Whether or not highlight is enabled. (default: true)
algolia_searchable_post_records array $records, WP_Post $post
algolia_searchable_post_{$post_type}_records array $records, WP_Post $post
algolia_should_filter_query bool $should_filter, WP_Query $query
algolia_theme_templates_dirname string $path Name of the folder to look for in the active theme
algolia_ua_integration_name string $value Name to use for the integration user agent
algolia_ua_integration_version string $value Version use for the integration user agent
algolia_watch_post_meta_keys array $keys, int $object_id. Array of post meta keys to watch for changes on to trigger update
algolia_watch_term_meta_keys array $keys, int, $object_id, Array of term meta keys to watch for changes on to trigger update
algolia_watch_user_meta_keys array $keys, int, $object_id, Array of user meta keys to watch for changes on to trigger update
algolia_custom_template_location null $value, string $file Custom server path to load a template file from. Second parameter is the template file being requested.

algolia_custom_template_location is available in version 2.5.0 and later.