Open danielmilner opened 5 years ago
Code samples for adding Basic Auth to both Admin Ajax and Cron requests.
Admin Ajax
add_filter( 'http_request_args', 'http_request_args_basic_auth', 10, 2 );
function http_request_args_basic_auth( $args, $url ) {
if ( strpos( $url, admin_url( 'admin-ajax.php' ) ) === 0 ) {
$args['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
}
return $args;
}
Cron
add_filter( 'cron_request', 'cron_request_basic_auth' );
function cron_request_basic_auth( $cron_request ) {
$cron_request['args']['headers']['Authorization'] = 'Basic ' . base64_encode( USERNAME . ':' . PASSWORD );
return $cron_request;
}
It is fairly common for sites that are in development to be behind Basic Authentication. In these instances, WP Cron jobs and Admin Ajax requests will fail. We should add a setting to add the Basic Auth username and password to requests.