WordPress – WP Cronjob behind HTTP Authentication

WP Cronjobs doesn’t work behind HTTP Authentication over .htaccess Rules. To reaktivate WP Cron follow the steps:

Add Lines to wp-config.php:

if(!defined('WP_CRON_CUSTOM_HTTP_BASIC_USERNAME')) {
    define('WP_CRON_CUSTOM_HTTP_BASIC_USERNAME', 'YOUR_USERNAME_HERE');
}

if(!defined('WP_CRON_CUSTOM_HTTP_BASIC_PASSWORD')) {
    define('WP_CRON_CUSTOM_HTTP_BASIC_PASSWORD', 'YOUR_PASSWORD_HERE');
}

If does not exist, create directory wp-content/mu-plugins/ and add follow PHP-Script:

<?php

/*
Plugin Name: HTTP Basic Cron Request
Description: Run Cron Request behind HTTP Authentification
Author: Nick Ohrn
Version: 1.0
Author URI: www.nickohrn.com
*/

if(defined('WP_CRON_CUSTOM_HTTP_BASIC_USERNAME') && defined('WP_CRON_CUSTOM_HTTP_BASIC_PASSWORD')) {
        function http_basic_cron_request($cron_request) {
                $headers = array('Authorization' => sprintf('Basic %s', base64_encode(WP_CRON_CUSTOM_HTTP_BASIC_USERNAME . ':' . WP_CRON_CUSTOM_HTTP_BASIC_PASSWORD)));

                $cron_request['args']['headers'] = isset($cron_request['args']['headers']) ? array_merge($cron_request['args']['headers'], $headers) : $headers;

                return $cron_request;
        }

        add_filter('cron_request', 'http_basic_cron_request');
}

?>

Restart Web-Server service apache2 restart.

All scripts created by Nick Ohrn and modified a little bit by myself! See comments in the script.

Was this helpful?

0 / 0