Exercise 2: Should a New-Comment Notification Email Be an Action or a Filter? — Possible Solution ==================================================================== THE ANSWER: AN ACTION ------------------------------ Sending a notification email when a comment is posted should be built as an action, using add_action(), not a filter. WHY, PER THIS CHAPTER'S OWN COMPARE TABLE ------------------------------ Per this chapter's own table, actions "let you DO something at a specific point," with an explicit example of "sending an email when a post is published" - functionally the same category of task as sending an email when a comment is posted. Filters, by contrast, "let you MODIFY a piece of data as it passes through," with the hooked function required to return a value that WordPress then actually uses in place of the original. WHY SENDING AN EMAIL DOESN'T FIT THE FILTER DEFINITION ------------------------------ Sending an email is not modifying any piece of data that WordPress is currently processing and needs back - there's no "value" being passed through that the email-sending code would need to receive, alter, and return. It's a side effect - doing something (dispatching an email) in response to an event (a new comment) - which matches the action definition precisely: something happens, but nothing about WordPress's own internal data needs to be changed or handed back as a result. WHY USING A FILTER FOR THIS WOULD BE THE WRONG TOOL ------------------------------ If this were built as a filter instead, the function would be expected to return some specific value on the hook it's attached to - hooking onto something comment-related as a filter would require the developer to figure out what data to return, entirely unrelated to the actual goal of just sending an email. Worse, per this chapter's own warning about filters, forgetting to return the correct value from a mis-designed filter risks silently corrupting whatever data that filter was supposed to be passing through - a real risk this scenario has no reason to introduce, since nothing needs modifying here at all. THE PRACTICAL IMPLEMENTATION ------------------------------ This would be built with add_action(), hooked onto WordPress's own comment-related action (such as one that fires when a comment is successfully inserted), with the hooked function simply calling WordPress's mail-sending functionality - a direct application of this chapter's own "doing something at a specific point" pattern. WHY THIS WORKS AS AN ANSWER ------------------------------ It states the correct choice and justifies it directly against this chapter's own explicit action/filter definitions and worked email example, and explains concretely why the filter's own return-value requirement doesn't apply to a pure side-effect task like sending an email.