wp_schedule_event only when admin is visited

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.

Related posts

Leave a Reply

1 comment

  1. 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
        }
    }