How to schedule one-time tasks to be executed at a specified future time
To schedule one-time tasks in WordPress follow these steps:
1. Determine the Time: Calculate the future timestamp for when you want the task to run.
2. Define the Hook: Choose a unique action hook name that will trigger your task.
3. Create the Function: Write the function that will execute when the event is triggered.
4. Schedule the Event: Use wp_schedule_single_event
to schedule the event with the calculated timestamp, hook and any arguments.
//Step 1: Calculate the future timestamp.
$timestamp = time() + 3600; // 1 hour in second
//Step 2: Define the action hook
$hook = 'my_custom_event';
//Step 3: Create the function to execute
function my_custom_function( $args ) {
//Code to execute
error_log( 'The event has run with argument: ' .$args['example_arg'] );
}
//Step 4: Schedule the event
$args = array(' example_arg' => 'value' );
wp_schedule_single_event( $timestamp, $hook, $args );
//Hook the function to the event
add_action( $hook, 'my_custom_function' );
This method allows you to effectively manage one-time tasks in WordPress.
6 Comments
Shawnee
November 4, 2024I every time used to read piece of writing
in news papers but now as I am a user of web therefore
from now I am using net for content, thanks to web.
Nancy
November 5, 2024I get pleasure from, lead to I found just what I used
to be taking a look for. You have ended my four day long hunt!
God Bless you man. Have a great day. Bye
Saikat Mondal
November 9, 2024Thank you so much @Nancy
Zac
November 8, 2024I could not refrain from commenting. Well written!
Saikat Mondal
November 9, 2024Thank you @Zac