Skip to content

Instantly share code, notes, and snippets.

@thenbrent
Last active December 2, 2022 17:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thenbrent/9c3aaa829fc1a6399008944040b88e0a to your computer and use it in GitHub Desktop.
Save thenbrent/9c3aaa829fc1a6399008944040b88e0a to your computer and use it in GitHub Desktop.
Snippet to create scheduled actions with Action Scheduler via $_GET params.
<?php
/**
* Check for $_GET params on admin pages and use them to create corresponding actions.
*
* Example URL string to append to any admin page: &as_add_actions=single&hook=my_action&time=1563420973
*/
function as_test_schedule_async_action() {
if ( ! isset( $_GET['as_add_actions'] ) ) {
return;
}
$action_type = $_GET['as_add_actions'];
$time = ( isset( $_GET['time'] ) ) ? $_GET['time'] : gmdate( 'U' );
$hook = ( isset( $_GET['hook'] ) ) ? $_GET['hook'] : sprintf( 'test_%s_action', $action_type );
switch ( $action_type ) {
case 'async' :
for( $i = 1; $i < 10; $i++ ) {
as_enqueue_async_action( $hook, array( 'timestamp' => time(), 'i' => $i ), 'async' );
}
break;
case 'cron' :
as_schedule_cron_action( $time, '*/10 * * * *', $hook );
break;
case 'recurring' :
as_schedule_recurring_action( $time, 5 * MINUTE_IN_SECONDS, $hook );
break;
case 'single' :
as_schedule_single_action( $time, $hook );
break;
}
}
add_action( 'admin_init', 'as_test_schedule_async_action' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment