Skip to content

Instantly share code, notes, and snippets.

@sharazghouri
Created December 24, 2017 17:08
Show Gist options
  • Save sharazghouri/48b702a18c97e4f98a758bca39447f4d to your computer and use it in GitHub Desktop.
Save sharazghouri/48b702a18c97e4f98a758bca39447f4d to your computer and use it in GitHub Desktop.
Cron task plugin ( event schedule when plugin active and unschedule when decactive [ use WP Crontrol to see scheduled event ] )
<?php
/*
Plugin Name: My WP-Cron Test
*/
//activation of job
register_activation_hook(__FILE__, 'my_activation');
add_action('hourly_job', 'do_hourly_job');
function do_hourly_job() {
?>
<script>
alert();
</script>
<?php
}
function my_activation() {
if (! wp_next_scheduled ( 'hourly_job' )) {
$result = wp_schedule_event(time(), 'hourly', 'hourly_job');
//var_dump( $result );
}
}
//deactivation job
register_deactivation_hook( __FILE__, 'bl_deactivate' );
function bl_deactivate() {
$timestamp = wp_next_scheduled( 'hourly_job' );
$result= wp_unschedule_event( $timestamp, 'hourly_job' );
// var_dump( $result , $timestamp );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment