• I have written a code snippet that I’d like to run only once a day. Is this possible to do within code snippets or better to run a cron job?

Viewing 1 replies (of 1 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    It’s possible to create a cron job within a snippet:

    add_action( 'init', function () {
    
    	$hook = 'run_snippet_daily';
    	$args = array();
    
    	if ( ! wp_next_scheduled( $hook, $args ) ) {
    		wp_schedule_event( time(), 'daily', $hook, $args );
    	}
    } );
    
    add_action( 'run_snippet_daily', function () {
    	
    	// do something once each day
    } );

    Once you’ve added the first bit as a snippet, then you can keep using the original hook in future snippets.

Viewing 1 replies (of 1 total)
  • The topic ‘How to schedule a snippet to run once a day’ is closed to new replies.