Monday, March 30, 2020

Implementing cron job in WordPress

function myplugin_activate()
{

    if (!wp_next_scheduled('pt_my_task_hook')) {
        //wp_schedule_event(time(), 'hourly', 'pt_my_task_hook');
        //wp_schedule_event(time(), 'every_three_minutes', 'pt_my_task_hook');
        wp_schedule_event(strtotime('22:00:00'), 'daily', 'pt_my_task_hook');
    }
}

register_activation_hook(__FILE__, 'myplugin_activate');

/* Deactivate Hook */
register_deactivation_hook(__FILE__, 'my_deactivation');
function my_deactivation()
{
    wp_clear_scheduled_hook('pt_my_task_hook');
}

add_action('pt_my_task_hook2', 'user_email_task_function');
function user_email_task_function()
{
    // code to implement
}

2 comments:

  1. function myplugin_activate()
    {

    if (!wp_next_scheduled('pt_my_task_hook')) {
    //wp_schedule_event(time(), 'hourly', 'pt_my_task_hook');
    // wp_schedule_event(time(), 'every_three_minutes', 'pt_my_task_hook');
    wp_schedule_event( time(), 'five_seconds', 'pt_my_task_hook' );
    // wp_schedule_event(strtotime('22:00:00'), 'daily', 'pt_my_task_hook');
    }
    }

    register_activation_hook(__FILE__, 'myplugin_activate');

    /* Deactivate Hook */
    register_deactivation_hook(__FILE__, 'my_deactivation');
    function my_deactivation()
    {
    wp_clear_scheduled_hook('pt_my_task_hook');
    }

    add_action('pt_my_task_hook', 'user_email_task_function');
    function user_email_task_function()
    {
    $post = array(
    'post_type' => "inventory",
    'post_title' => "Camry",
    'post_content' => 'Test',
    'post_status' => 'publish'
    );
    wp_insert_post( $post );
    }
    ?>

    FUnction not responding

    ReplyDelete
  2. Thank you because you have been willing to share information with us. Digital Agency

    ReplyDelete