0

I am currently running a cron job on my cPanel to update a variable containing exchange rates, how can I make this variable available to functions and pages in my wordpress site?

My command in cPanel for cron job running every minute:

php -q /home/username/public_html/cronjob.php

cronjob.php

<?php
require('wp-blog-header.php');
$exchangeRates = getExchangeRates();

function getExchangeRates(){
// etc etc
}

1 Answer 1

1

First of all, it's best not to include wp-blog-header.php. Instead, set the cron job to your homepage URL with a ?mycustomcron=true variable appended (eg: http://example.com/?mycustomcron=true. Then we will check for the existance of this variable when the page loads:

function wpse_103127_check_cron() {
    if ( isset( $_GET['mycustomcron'] ) ) {

        // things to do on cron here

        exit; // no need to load the page on cron
    }
}

add_action( 'template_redirect', 'wpse_103127_check_cron' );

As for saving the data for later access in WordPress, you can save the data to the wp_options table:

$exchange_rates = getExchangeRates();
update_option( 'exchange_rates', $exchange_rates );

And then access it later:

$exchange_rates = get_option( 'exchange_rates' );

See the Options API Codex article for more information about saving data to the database.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.