Jul.17

WordPress cron job for updating your task automatic in 5 min time basis

here is code used for Cron job.

function my_cron_schedules($schedules) {
if (!isset($schedules[“5min”])) {
$schedules[“5min”] = array(
‘interval’ => 5 * 60,
‘display’ => __(‘Once every 5 minutes’));
}
if (!isset($schedules[“30min”])) {
$schedules[“30min”] = array(
‘interval’ => 30 * 60,
‘display’ => __(‘Once every 30 minutes’));
}
return $schedules;
}

 

add_filter(‘cron_schedules’, ‘my_cron_schedules’);

 

 

if (!wp_next_scheduled(‘my_schedule_hook’)) {
wp_schedule_event(time(), ‘5min’, ‘my_schedule_hook’);
}
add_action(‘my_schedule_hook’, ‘my_task_function’);

 

function my_task_function() {
/*
*  Check user FFl date crossed current time or not
*  IF matched any user of FFL expired date
*
*/

$args_new = array(
‘meta_query’ => array(
‘relation’ => ‘AND’,
array(
‘key’ => ‘wpcf-is_ffl_verified’,
‘value’ => ‘1’,
‘compare’ => ‘=’
),
array(
‘key’ => ‘wpcf-ffl-expiration-date’,
‘value’ => time(),
‘compare’ => ‘<=’
)
)
);
// The Query
$user_query = new WP_User_Query($args_new);

// User Loop
if (!empty($user_query->get_results())) {
foreach ($user_query->get_results() as $user) {
/*
* Set all post not FFL verified
*/
$user_id = $user->ID;
$exp = get_user_meta( $user_id, ‘wpcf-ffl-expiration-date’,true);

$args_get_post = array(
‘post_type’ => array(‘firearms’),
‘author’ => $user_id
);
// Current User post update in to FFL Verified yes
$current_user_posts = query_posts($args_get_post);

foreach ($current_user_posts as $row) {
update_post_meta($row->ID, ‘ffl_verified’, ‘0’);
}

/*
*
* set user is no more FFL verified
* New post FFL is depends on this value
*
*/
update_user_meta($user_id, ‘wpcf-is_ffl_verified’,’0′);

/*
* See the post type older request form value
* and send it to trash
*/
$args_user_ffl = array(
‘post_type’ => ‘user-ffl’,
‘author’ =>$user_id
//’post_status’ => array(‘publish’, ‘pending’, ‘draft’)
);
$wp_ffl_posts = get_posts($args_user_ffl);

/*
*
* remove previous requested user FFL information
* may be set to pending
*
*/
if (count($wp_ffl_posts)>0){
wp_trash_post($wp_ffl_posts[0]->ID);
}

/*
*  Email format sent to user
*
*/
$to = trim($user->user_email);

$expired_readable = date(‘F ,j Y’, $exp);

$subject = ‘FFL Expired  !’;
$body = ‘<br/> Hello ‘.$user->user_nicename.’! <br/>’
. ‘Your FFL is expire on ‘ . $expired_readable . ”
. ‘<br/>Your all Firearms post will be invisible on site. ‘
. ‘<br/>You are requested to submit valid FFL for continuing to publish Firearms posts.’
. ‘<br/><br/>’
. ‘Thanks ‘
. ‘<br/>’
. ‘Web Admin <br/>’ .get_site_url();
$headers = array(‘Content-Type: text/html; charset=UTF-8’);

wp_mail($to, $subject, $body, $headers);
}
}
}

 

 

 

 

 

Share this Story:
  • facebook
  • twitter
  • gplus

Leave a comment

Facebook Comment