1

On the documentation page it says "The action will trigger when someone visits your WordPress site, if the scheduled time has passed."

Is it possible to make this action trigger only when the admin is visited? I am trying to prevent a slow operation from impacting users on the front side.

  • 1
    I know this question is old, but I think it assumes something that is not true. WordPress "cron jobs" are designed to avoid impacting normal requests with expensive operations. Every time someone visits your WordPress website, a separated, non-blocking request is initiated. The cron job is executed in the second request, while the first one continues generating the content of the page normally. Here is a good explanation of how WordPress Cron Jobs work: sitepoint.com/wordpress-scheduling. – Willington Vega Oct 13 '14 at 0:48
2

In your event callback function check to see if the user id the admin then run the function else just reschedule it. So using the example from the codex's page you linked in the question it would be something like this:

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

function do_this_hourly() {
    // do something every hour only if this is the admin
    if (!current_user_can('administrator')){
        // time()+1800 = 1/2 hour from now.
        wp_schedule_single_event(time()+1800, 'my_hourly_event');
        return;
    }else{
        //do your thing
    }
}
  • so just a check of is_admin() will not suffice? – Mike Nov 16 '11 at 15:07
  • depends if you have users that go into the admin pages or not, if its not a problem then yes a simple is_admin would do just fine – Bainternet Nov 16 '11 at 15:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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