Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduction to WP-Cron #3018

Open
jonathanbossenger opened this issue Nov 9, 2024 · 9 comments
Open

Introduction to WP-Cron #3018

jonathanbossenger opened this issue Nov 9, 2024 · 9 comments
Assignees

Comments

@jonathanbossenger
Copy link
Collaborator

Details

  • Content type (Online Workshop, Lesson, Course, Tutorial, or Lesson Plan): Lesson
  • Content title: Introduction to WP-Cron
  • Topic description: Introduce WP-Cron, why we need it, and how it works
  • Audience (User, Developer, Designer, Contributor, etc.): Developer
  • Experience Level (Beginner, Intermediate, Advanced, Any): Intermediate

Learning Objectives

  • Explain what WP-Cron is
  • Differentiate between WP-Cron and a server cron
  • Explain why we need WP-Cron
  • Briefly describe the request flow of a WP-Cron task

Related Resources and Other Notes

Automation Code

//lesson

Copy link
Contributor

github-actions bot commented Nov 9, 2024

Lesson Development Checklist

  • Gather any relevant links to Support, Docs, or related material
  • Description and Objectives finalized
  • Lesson created and announced to the team for review
  • Lesson reviewed
  • Lesson video submitted and published to WPTV
  • Lesson created on Learn.WordPress.org
  • Lesson video published to YouTube
  • Lesson on Learn.WordPress.org updated with YouTube video
  • Lesson published to Learn.WordPress.org

@jonathanbossenger
Copy link
Collaborator Author

@CrochetFeve0251 In reviewing the Cron module, I realized I left out an important introduction lesson, so I've added it here with the lesson description/objectives. Would you be okay with starting with this one first?

@CrochetFeve0251
Copy link

@jonathanbossenger Yes that would be fine for you

@CrochetFeve0251
Copy link

CrochetFeve0251 commented Nov 23, 2024

I added a first version from the script: wptrainingteam/learn-wp-content#18

Tell me if you think I should add more on these two points:

  • Explain why we need WP-Cron
  • Briefly describe the request flow of a WP-Cron task

@jonathanbossenger
Copy link
Collaborator Author

Thanks @CrochetFeve0251. I will be reviewing the script this week, and I'll get back to you.

@github-project-automation github-project-automation bot moved this to 👋 Ready to Create in LearnWP Content - Development Dec 11, 2024
@kaitohm kaitohm moved this from 👋 Ready to Create to 🚧 Drafts in Progress in LearnWP Content - Development Dec 11, 2024
@jonathanbossenger
Copy link
Collaborator Author

@jonathanbossenger jonathanbossenger moved this from 🚧 Drafts in Progress to 🔎 Ready for Review in LearnWP Content - Development Dec 16, 2024
@digitalchild
Copy link
Contributor

digitalchild commented Dec 20, 2024

When you switch to talking about cron in general, I wonder if there needs to be more of a bridging sentence as to why this is relevant in the relation to wp-cron. You do mention this at the end of the section, but I'm wondering if it should start with this?

  • Learning outcomes/objectives are clear.
  • Technical concepts introduced in the content are accurate.
  • The speed of demonstrations are easy to follow.
  • The narration audio matches what is shown visually.
  • Spelling and grammar are correct.
  • Sound quality is consistent throughout the video.
  • Brand Usage Guidelines and Promotional Guidelines are being followed.
  • Media assets are all in the public domain (CC0).

@benazeer-ben
Copy link

Tutorial/Lessons Review Checklist

Please tick all items you've confirmed:

  • Learning outcomes/objectives are clear.
  • Technical concepts introduced in the content are accurate.
  • The speed of demonstrations are easy to follow.
  • The narration audio matches what is shown visually.
  • Spelling and grammar are correct.
  • Sound quality is consistent throughout the video.
  • Brand Usage Guidelines and Promotional Guidelines are being followed.
  • Media assets are all in the public domain (CC0).

The script covers all necessary points, and is easy to understand.

Here are a few additional points that I believe improve the script, making it more suitable for developer documentation:

Introduction to WP Cron

WP-Cron is WordPress's built-in task scheduling system. It allows developers to schedule tasks (e.g., publishing scheduled posts, sending emails, or running periodic updates) without relying on server-level cron jobs.

WP-Cron is not a "real" cron system. Instead, it relies on website traffic to trigger scheduled tasks.

How WP-Cron Works: Request Flow

  • Task Scheduling: A task is scheduled using wp_schedule_event() or similar functions.
  • Task Storage: The task is stored in the WordPress database (wp_options table with the cron key).
  • Triggering: When a user visits the site, WordPress checks if any scheduled tasks are due.
  • Execution: If a task is due, WordPress runs it by making an HTTP request to itself (a "cron request").
  • Completion: The task is executed, and the schedule is updated for the next run.

Example: Scheduling a Task with WP-Cron

// Schedule a recurring event
if (!wp_next_scheduled('my_custom_task_hook')) {
    wp_schedule_event(time(), 'hourly', 'my_custom_task_hook');
}

// Hook the task to a function
add_action('my_custom_task_hook', 'my_custom_task_function');

function my_custom_task_function() {
    // Task logic here
    error_log('Custom task executed at ' . current_time('mysql'));
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔎 Ready for Review
Development

No branches or pull requests

4 participants