WebDevStudios / wp-search-with-algolia

Improve search on your site. Autocomplete is included, along with full control over look, feel and relevance.
https://wordpress.org/plugins/wp-search-with-algolia/
138 stars 54 forks source link

Allow for adding extra headers to PHP API config. #415

Closed tw2113 closed 1 month ago

tw2113 commented 2 months ago

In the vein of https://github.com/WebDevStudios/wp-search-with-algolia/pull/407/files lets allow for customizing defaultHeaders.

Add to our code area:

if ( is_array( $custom_config['DefaultHeaders'] ) && ! empty( $custom_config['DefaultHeaders'] ) ) {
    $config->setDefaultHeaders( $custom_config['DefaultHeaders'] );
}

Example usage:

function wds_algolia_custom_searchclient_config( $config ) {

    $ip_address = $_SERVER['HTTP_X_FORWARDED_FOR'] ?? $_SERVER['REMOTE_ADDR'];

    $headers = [
        'X-Forwarded-For' => $ip_address,
    ];
    $config['DefaultHeaders'] = $headers;

    return $config;
}
add_filter( 'algolia_custom_search_config', 'wds_algolia_custom_searchclient_config' );

See: https://wordpress.org/support/topic/how-to-set-extra-header/

tw2113 commented 1 month ago

This has been merged into the release290 branch.