Skip to content

Instantly share code, notes, and snippets.

@jchristopher
Last active July 27, 2021 17:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jchristopher/bd090efd7a803a1b8e362b05d6e2e923 to your computer and use it in GitHub Desktop.
Save jchristopher/bd090efd7a803a1b8e362b05d6e2e923 to your computer and use it in GitHub Desktop.
Provide HTTP Basic Authentication credentials to SearchWP (and WP Cron)
<?php
// Provide HTTP Basic Authentication credentials to SearchWP (and WP Cron).
class MySearchWPBasicAuthCreds {
private $username = 'username'; // HTTP Basic Auth username.
private $password = 'password'; // HTTP Basic Auth password.
function __construct() {
// Provide HTTP Basic Authentication credentials to SearchWP.
add_filter(
'searchwp\background_process\http_basic_auth_credentials',
function( $credentials ) {
return [
'username' => $this->username,
'password' => $this->password,
];
}
);
// Also provide HTTP Basic Authentication credentials to WP Cron.
// This can be removed if handled elsewhere, otherwise *REQUIRED*
add_filter( 'cron_request', function( $cron_request ) {
if ( ! isset( $cron_request['args']['headers'] ) ) {
$cron_request['args']['headers'] = [];
}
if ( isset( $cron_request['args']['headers']['Authorization'] ) ) {
return $cron_request;
}
$cron_request['args']['headers']['Authorization'] = sprintf(
'Basic %s',
base64_encode( $this->username . ':' . $this->password )
);
return $cron_request;
}, 999 );
}
}
new MySearchWPBasicAuthCreds();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment