无需插件在WordPress中配置cronjob运行定时发邮件任务的方法
Hey there! Let's walk through exactly how to set up your weekly and monthly fan email cron jobs in WordPress—no plugins required. I’ll break this down into simple, actionable steps so you can get your emails firing on schedule.
First, a quick heads-up: WordPress Cron isn’t the same as server-level cron. It triggers when someone visits your site (so low-traffic sites might have delayed tasks). If you need rock-solid reliability later, you can pair it with a server cron job, but we’ll stick to core WordPress tools for now.
WordPress comes with built-in schedules like hourly, daily, and weekly—but it doesn’t include a monthly option out of the box. Let’s fix that first.
Add this code to your theme’s functions.php file (or a custom plugin, which is more stable if you switch themes later):
function custom_cron_schedules( $schedules ) { // Add a monthly schedule (30 days in seconds; adjust if you need exact calendar months) $schedules['monthly'] = array( 'interval' => 2592000, // 30 * 24 * 60 * 60 = 2,592,000 seconds 'display' => __( 'Once Monthly' ), ); return $schedules; } add_filter( 'cron_schedules', 'custom_cron_schedules' );
Note: If you need exact calendar-month timing (e.g., 1st of every month), you’d need a bit more logic, but this 30-day interval works for most use cases.
Next, we’ll register hooks for your emails and schedule them to run on your desired cadences. This code checks if the jobs are already scheduled to avoid duplicates:
function schedule_fan_email_jobs() { // Schedule weekly email if ( ! wp_next_scheduled( 'send_weekly_fan_email' ) ) { wp_schedule_event( time(), 'weekly', 'send_weekly_fan_email' ); } // Schedule monthly email if ( ! wp_next_scheduled( 'send_monthly_fan_email' ) ) { wp_schedule_event( time(), 'monthly', 'send_monthly_fan_email' ); } } add_action( 'wp', 'schedule_fan_email_jobs' );
wp_schedule_event()takes three arguments: start time (time()= right now), schedule name (weekly/monthly), and a unique hook name.- We hook this to
wpso it runs when a frontend or admin page loads, but only once per job.
Now tie the custom cron hooks to the email functions you’ve already written. Let’s say your functions are named send_weekly_fan_newsletter() and send_monthly_fan_update()—add this code:
// Connect weekly hook to your email function add_action( 'send_weekly_fan_email', 'send_weekly_fan_newsletter' ); // Connect monthly hook to your email function add_action( 'send_monthly_fan_email', 'send_monthly_fan_update' );
That’s it! When the cron schedule triggers the hook, your email function will run automatically.
Want to make sure everything’s working? Here are a few ways to check:
- View scheduled events: Add this temporary code to
functions.phpto see all cron jobs in your admin dashboard (remove it after testing):
Log into your WP admin, and you’ll see a list of all scheduled jobs—look for yourfunction debug_cron_events() { if ( current_user_can( 'manage_options' ) ) { echo '<pre>'; print_r( wp_get_scheduled_events() ); echo '</pre>'; } } add_action( 'admin_notices', 'debug_cron_events' );send_weekly_fan_emailandsend_monthly_fan_emailentries. - Manually trigger a job: Run
do_action('send_weekly_fan_email');in a temporary admin page or via WP-CLI to test if your email function sends correctly. - Fix low-traffic delays: If your site doesn’t get many visitors, set up a server cron job to trigger WP Cron every 15 minutes. For Linux servers, add this to your crontab:
*/15 * * * * wget -q -O - https://yourdomain.com/wp-cron.php?doing_wp_cron >/dev/null 2>&1
- Use a custom plugin: Instead of adding code to
functions.php, create a tiny custom plugin (just a PHP file with plugin headers) to hold this cron logic. That way, it won’t break if you switch themes. - Check error logs: Enable
WP_DEBUGinwp-config.phpto catch any issues with your email functions or cron setup. - Unschedule jobs if needed: If you want to stop a job later, use
wp_unschedule_event( wp_next_scheduled( 'send_weekly_fan_email' ), 'send_weekly_fan_email' );.
内容的提问来源于stack exchange,提问作者Quang Hoang

