WordPress execute specific cronjob on specific Page

Issue

I am having a small tour page that uses built-in cron jobs (wp-cron) to sync tours and sessions via an API every 4 hours and until here everything is perfect.

enter image description here

Now, what I am trying to achieve now is to also and "forcefully" execute this specific cronjob (regardless of it’s schedule) when being on a specific page, e.g. Thank you page (not Woocommerce).

What’s the best way to have this achieved? Some expert help would be greatly appreciated, thank you.

Solution

I think you can try by using admin_init hooks and check the page id or url then run the execute function.

add_action( 'travel_booking_do_tour_search_action', 'my_custom_cron_function');

function my_custom_cron_function(){
    // your execute action
}

add_action( 'admin_init', 'execute_cron_on_specific_page' );

function execute_cron_on_specific_page{
    if( is_page( 2094 ) ) // page_id = 2094 (eg:thank you page)
    {
        my_custom_cron_function();
    }
}

Answered By – Lwin Ko Ko

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published