Search code examples
wordpressfunctioncpanelcron-task

wordpress live site : suggestion to schedule daily jobs via cpanel


I have a wordpress site live , I would like to schedule a daily job via cpanel to feed a custom stats table. I am a new to the concept of croning job. I would like some suggestion, also I noticed that a lot of tutorials talks about a file wp-cron.php do i need to schedule the job in wordpress as explained bellow and then replace the wordpress cron with the cpanel cron? or I can directly create a cpanel cron?

wordpress cron


register_activation_hook(__FILE__, 'my_activation');

function my_activation() {
    if (! wp_next_scheduled ( 'my_hourly_event' )) {
    wp_schedule_event(time(), 'hourly', 'my_hourly_event');
    }
}

add_action('my_hourly_event', 'do_this_hourly');

function do_this_hourly() {
    // do something every hour
}

Solution

  • To make a real Cron Job for your site with cPanel. First, create a file to execute your task. To do this, I'd recommend putting the file in the root directory of your account, rather than in the public_html folder. Create a file like, my-wp-cron.php

    <?php
    
    include dirname(__FILE__) . '/public_html/wp-load.php'; // Load WP Functions
    // If your path is different, use it instead.
    
    function my_cron_function(){
    
        // do your cron function here
    
        die();
    
    }
    
    my_cron_function();
    

    Then in your cPanel - look for "Cron Jobs" and setup something like this:

    /usr/local/bin/php /home/your_account_name/my-wp-cron.php

    If your path isn't /home/ it should show your the path to your files in the Cron Job manager in your cPanel.

    Set your cron schedule as you desire using the cPanel cron scheduler interface.