Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I wanted to use Wordpress built in WP Cron to update currency rates daily.

I use this function currently,

require_once(WP_PLUGIN_DIR . '/autocurupdate/currencyexchange_class.php');
$cx=new currencyExchange();
$cx->getData();
$currency_sql = "select last_updated from " . $wpdb->prefix."currencies where code = 'PHP'";
$currency_1 = mysql_query($currency_sql);
$currency_2 = mysql_fetch_array($currency_1);
$last_updated = mysql2date(get_option('date_format'), $currency_2['last_updated']);
$current_date = $cx->Date;
$currentdate = mysql2date(get_option('date_format'), $current_date);
if($last_updated !== $currentdate) {
update_currency();
}

How can I run update_currency() function daily using WP Cron?

share|improve this question
    
Ken, any progress on that question? –  kaiser Oct 29 '13 at 22:25

2 Answers 2

First things first: Say hello! to $wpdb

The wpdb Class is what you take to interact with the database on it's most basic level. Forget all those mysql_* functions, when it comes to WordPress. The core has a nice wrapper, that makes most things pretty safe for you (hint: SQL-injections and other bad boys). Or at least offers things, that you can use to make it safe (like_escape(), $wpdb->prepare() to mention some).


The Schedule API

WordPress comes with some sort of pseudo Cron Job:

  • Schedule API It's not that well documented, but this is what comes a real Cron Job closest. Make sure, that you read everything about it and try it in a clean install.

I have written a small plugin, that will allow you inspect what you're doing. It's called »WP Cron Jobs List« and is hosted on GitHub/Gist or via our own WPSE Plugin repository.

enter image description here

share|improve this answer

This is how works the cron

add_action('my_daily_event', 'update_currency');
add_action('wp', 'my_activation');

function my_activation() {
if ( !wp_next_scheduled( 'my_daily_event' ) ) {
    wp_schedule_event( time(), 'daily', 'my_daily_event');
}
}

This should work. But wordpress cron works when someone visits your site and the scheduled time has been passed.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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