You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

无需插件在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.

1. Quick WordPress Cron Primer

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.

2. Add a Monthly Schedule (WP Doesn’t Have This By Default)

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.

3. Schedule Your Cron Jobs

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 wp so 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.

5. Test and Verify Your Cron Jobs

Want to make sure everything’s working? Here are a few ways to check:

  • View scheduled events: Add this temporary code to functions.php to see all cron jobs in your admin dashboard (remove it after testing):
    function 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' );
    
    Log into your WP admin, and you’ll see a list of all scheduled jobs—look for your send_weekly_fan_email and send_monthly_fan_email entries.
  • 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
    
6. Pro Tips
  • 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_DEBUG in wp-config.php to 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:17:06