1

I have created a plugin in WordPress to sync posts from a third party API.

I have set a cron job to fetch data in every 10 minutes. If I'm logged in, cron is working, otherwise cron is not working.

4
  • 2
    pls provide the code you used.
    – 吉 宁
    Commented Feb 13, 2017 at 12:26
  • function add_new_intervals($schedules) { if(get_option('shedule_cron') == 'shedule_cron'){ $interval = 31; } elseif (get_option('shedule_cron_next') != '') $interval = 61; else { $interval = 91; } // add weekly and monthly intervals $schedules['instCronSchedule'] = array( 'interval' => $interval, 'display' => __('custom time') ); return $schedules; }} Commented Feb 13, 2017 at 12:37
  • docs.google.com/document/d/… Commented Feb 13, 2017 at 12:44
  • 2
    plz add your code in question update, edit your qusetion and post your code Commented Feb 13, 2017 at 12:48

2 Answers 2

1

The WP Cron .. which runs when user hit website .. Thus if there are no website visits, WP Cron never runs.

Now you can use 2 solutions.

Disable WP-Cron and use real cron job and then custom real cron job

https://support.hostgator.com/articles/specialized-help/technical/wordpress/how-to-replace-wordpress-cron-with-a-real-cron-job

use custom interval in wp_schedule_event

add_filter( 'cron_schedules', 'myprefix_add_a_cron_schedule' );

function myprefix_add_a_cron_schedule( $schedules ) {

    $schedules['sixsec'] = array(

        'interval' => 21600, // Every 6 hours

        'display'  => __( 'Every 6 hours' ),
    );

    return $schedules;
}

     ///////Schedule an action if it's not already scheduled

if ( ! wp_next_scheduled( 'myprefix_curl_cron_action' ) ) {

    wp_schedule_event( time(), 'sixsec', 'myprefix_curl_cron_action' );
}

///Hook into that action that'll fire sixhour
 add_action( 'myprefix_curl_cron_action', 'import_into_db' );
2
  • I agree, a real cron job is needed. Must also pay particular attention to the cron job user as it may not be the same as the wordpress admin user. Commented Jan 28, 2020 at 2:35
  • 1
    Another very likely cause here is that the plugin is checking that an admin user is logged in. When cron runs, no user is logged in, so that check fails and the job doesn't run.
    – Brian C
    Commented Feb 24, 2022 at 2:47
-1

WordPress Import Cron is not working properly. If admin login, then only the crons are scheduled. This is default thing in WordPress. So you need to change your cron schedule to system task manager. This one is the possible way to run your cron without admin login.

Get the steps from following documentation URL. Configure It.

Wp-Cron.php Documentation

2
  • This is plugin no for layman not for geek.So layman not able to add manual cron. Commented Feb 13, 2017 at 12:44
  • The answer needs some code, and what's "Import Corn"? probably "Corn" should be "cron"? Commented Oct 4, 2023 at 8:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.