You have to schedule your event in a hook, for example in after_setup_theme
or wp
actions:
add_filter('cron_schedules','my_cron_definer');
function my_cron_definer($schedules){
$schedules['twomin'] = array(
'interval'=> 120,
'display'=> __('Once Every 2 Minutes')
);
return $schedules;
}
add_action('my_periodic_action','my_periodic_function');
function my_periodic_function(){
mail('[email protected]','Test!', 'Test Message');
}
add_action( 'wp', 'wpse8170_setup_events' );
// or add_action( 'after_setup_theme', 'wpse8170_setup_events' );
function wpse8170_setup_events() {
if ( !wp_next_scheduled( 'my_periodic_action' ) ) {
wp_schedule_event(time(), 'twomin', 'my_periodic_action');
}
}
Related Posts:
- Running WordPress on the Command Line – Turn off Delayed Output?
- How is WP Cron Locking implemented?
- Better handling of WP-CRON server load abuse
- When does next Cron Job run (time from now)?
- run a cron task without obstructing page load?
- How to make wp cron job not fire immediately?
- Trigger a cron every 24h GMT -8
- WordPress Cron Schedule the if and else statement
- Schedule event every second thursday of the month
- How do i schedule cron in wordpress for each second?
- WP Cron job every 1st and 15th of the month
- Template Tag not available in real Cron Jobs
- WordPress Caching – Transients API or “update_user_meta ” Cronjob?
- WP CRON on shared hosting that does not allow loopback connections?
- Is it possible to use `wp_schedule_event` with real cronjobs?
- How to use wp cron job to run a function
- Are uploads directories created on a schedule?
- Create wp_cront events dynamically upon user submission
- WordPress can’t seem to handle tens of thousands of scheduled posts? Cron Breaks
- What is the curl error 52 “empty reply from server”?
- How to test wp_cron?
- How to debug WordPress “Cron” wp_schedule_event
- Check if function called by cron job
- Wp_Schedule_Event every day at specific time
- Uncaught Error: Call to undefined function wp_generate_attachment_metadata() @ wp-cron
- Delete all scheduled events with a particular hook
- wp-cron.php – How are WP’s Cron transients removed?
- Need to execute a cron job
- crontab wp-cron.php on multisite path-based network: one for each path? Or just one for the root?
- What happens when wp_cron is deactivated in WordPress?
- Running wp-cron from CLI
- How Do I Make WordPress Run an Event Every Day?
- Cron jobs for deactivated plugins
- Why?: hundreds of empty files named wp-cron.php?doing_wp_cron.
- WP Cron emails not working
- Run function at specific time
- Scheduled Posts and wp-cron – Why don’t scheduled posts publish if too old?
- Check if event was scheduled – schedule event only once
- How do I get a Function in my functions.php to execute with a cron job?
- How to notify the admin about something that happened during a cron job
- Create function in functions.php with hook name to execute URL
- How to remove unused avatar uploads in buddypress [closed]
- cron.php being constantly deleted [closed]
- “Error establishing database connection” when trying to run script through crontab
- How to Better Control WordPress Cron Jobs?
- Detect if Cron is Running
- Run function with linux cron job in WordPress
- Schedule cron don’t work
- Auto Publish On Cron
- WordPress Cron how to find out my event recurrence?
- How to use a class within a cron job function
- Can a scheduler be set on submit of a form in wordpress?
- When to use wp_schedule_single_event for async / non-blocking processes?
- Issues When Recursively Calling wp_schedule_single_event()
- How does task scheduler plugin implements cron that is not dependent on page load request? [closed]
- How wp-cron can run in background if PHP is single threaded?
- System Cron job not firing
- WP Cron job timeout issues
- WordPress Cron Job Not working
- WordPress automatic publish: is there a timeout when WordPress won’t publish anymore?
- wp cron job fires at every second or so, if callback is wrapped with DOING_CRON check it never fires at all
- How to Trigger WP CRON at Local Timestamp?
- Cancel/Stop a currently ongoing scheduled cron event?
- Code does not work in class implementation but works fine in functions.php file
- Use cron to create a non blocking task
- How to create and run cronjob in WP without using the plugin?
- Scheduling WP cron jobs
- Switch from wp-cron to a server cron job
- WordPress cron is running with previously set time intervals and not the updated one
- is WGET correct for this cron job?
- How can I prove if wp cron is running my task if I have DISABLE_WP_CRON set to true
- pingbacks testing
- Best Way to Enter Maintenance Mode Programmatically
- WP Cron Working, but Function Not Working
- Solution for processing lots of data with CRON/API, dealing with memory/timeout issues
- Strange cron job behavior and how to solve it?
- WordPress cron not getting executed when called by external service (but ok from browser)
- Bulk updating a group of WordPress Pages every 10 minutes
- Cron job to change CPT
- Setting up a cron job to auto update a custom field
- Is there a delay to include call function modification in a scheduled cron task?
- How to pass variable from other function?
- How to Set a Condition via Page Template Name in WP Cron Job?
- Server cron job not working
- wp-cron event doesn’t run when custom login API is enabled
- Call a PHP file from Cronjob
- unix cronjob for wordpress does not stop running
- Run external file cron using WordPress Scheduler
- Better way to run heavy scripts using WordPress database
- does wordpress auto update work without a cron job?
- Pages for Cron use Only?
- Does DISABLE_WP_CRON prevent plugins from registering new cron tasks?
- Cronjob function not executed – negative seconds
- WP CRON Fails At 13:00 Every Day
- Running a cron job manually and immediately
- Cron job for let’s encrypt renewal
- Prevent duplicate cron jobs running
- Update last created post in custom post types with wp_cron()?
- Error code 499 on specific cron job
- Creating a background process that doesn’t need HTTP