I have a php script that executes a function and creates an xml file with results. I want this script runs only once daily and i'm using wordpress cron to do this. Problem is this script always runs on all pages load and this is bad because it takes more than 30 sec to complete. How can i stop it always runs and make it works only with wp-cron ? I read about cronjob but my host hasn't this feature. This is the code i use at top of my php file :
add_action( 'my_cron_hook', 'myfunction' );
if( !wp_next_scheduled( 'my_cron_hook' ) ) {
wp_schedule_event( time(), 'daily', 'my_cron_hook' );
}
register_deactivation_hook( __FILE__, 'my_deactivate' );
function my_deactivate() {
$timestamp = wp_next_scheduled( 'my_cron_hook' );
wp_unschedule_event($timestamp, 'my_cron_hook' );
}
I want script runs daily and always in background , it should not affect user navigation. Thanks.
Edit.
This is all code script :
add_action( 'cO_cron_hook', 'remoteHeader' );
if( !wp_next_scheduled( 'cO_cron_hook' ) ) {
wp_schedule_event( time() + 24 * 60 * 60, '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' );
}
set_time_limit(300);
function remoteHeader($url){
$online = 'online';
$offline = 'offline';
$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, 10);
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 $httpcode;
}
/* create a dom document with encoding utf8 */
$domtree = new DOMDocument('1.0', 'UTF-8');
/* create the root element of the xml tree */
$xmlRoot = $domtree->createElement("xml");
/* append it to the document created */
$xmlRoot = $domtree->appendChild($xmlRoot);
$currentSite = $domtree->createElement("site");
$currentSite = $xmlRoot->appendChild($currentSite);
/* you should enclose the following two lines in a cicle */
$site = remoteHeader("http://google.com/");
$currentTrack->appendChild($domtree->createElement('google',$site));
$site2 = remoteHeader("http://wordpress.com/");
$currentTrack->appendChild($domtree->createElement('wordpress',$site2));
...
header("Content-Type: text/plain");
/* get the xml printed */
echo $domtree->save('mytest.xml')