Execute php script daily with wordpress -


i found script check status of url. i'm trying integrate wordpress. code:

<?php /* plugin name: checkonline status version: 0.1 */  add_action( 'co_cron_hook', 'checkremoteservice' );  if( !wp_next_scheduled( 'co_cron_hook' ) ) { wp_schedule_event( time(), 'daily', 'co_cron_hook' ); }  register_deactivation_hook( __file__, 'bl_deactivate' );  function bl_deactivate() { $timestamp = wp_next_scheduled( 'co_cron_hook' ); wp_unschedule_event($timestamp, 'co_cron_hook' ); }  function checkremoteservice($atts) { extract(shortcode_atts(array(    'url' => 'http://',    'cache' => '600', // 60*10 (10 minutes)    'online' => 'online', // custom online msg    'offline' => 'offline' // custom online msg ), $atts));  $cachedstatus = 'cstatus_' . $url; $cachedposts = get_transient($cachedstatus); if ($cachedposts !== false) { return $cachedposts; } else {  // best change custom agent message // know requests coming from.  $agent = "mozilla/5.0 (compatible; msie 9.0; windows nt 6.1; win64; x64; trident/5.0)"; $ch = curl_init();  curl_setopt ($ch, curlopt_url,$url );  curl_setopt($ch, curlopt_useragent, $agent);  curl_setopt ($ch, curlopt_returntransfer, 1);  curl_setopt ($ch,curlopt_verbose,false);  curl_setopt($ch, curlopt_timeout, 5);  curl_setopt($ch, curlopt_nobody, 1);  curl_setopt($ch,curlopt_ssl_verifypeer, false);  curl_setopt($ch,curlopt_sslversion,3);  curl_setopt($ch,curlopt_ssl_verifyhost, false);  curl_exec($ch);    $httpcode = curl_getinfo($ch, curlinfo_http_code);    curl_close($ch);    if($httpcode >= 200 && $httpcode < 400) {     return $online;     } else {     return $offline;     }   set_transient($cachedstatus, $return, $cache);  return $return; } } add_shortcode('checkmyurl','checkremoteservice');  ?> 

what want run code once daily. how can make not run when refresh or visit page use shortcodes ? need display status of 50 sites , page takes more 10 sec load now. sorry simple question (i'm noob this) can't find solution. thanks.

edit. solved using http://wordpress.org/plugins/crony/

you can use wp_schedule_event() schedule recurring tasks.

wp_schedule_event()(documentation):

schedules hook executed wordpress actions core on specific interval, specified you. action trigger when visits wordpress site, if scheduled time has passed. see plugin api list of hooks.

to schedule event daily, you'd this:

function somefunc() {     wp_schedule_event( time(), 'daily', 'mydailyeventhook'); } 

hope helps!


Comments