Learn icon indicating copy to clipboard operation
Learn copied to clipboard

Determining the correct hook

Open jonathanbossenger opened this issue 1 year ago • 10 comments

Details

  • Content type (Online Workshop, Lesson, Course, Tutorial, or Lesson Plan): Lesson
  • Content title: Determining the correct hook
  • Topic description: How to determine which hook to use to achieve specific goals.
  • Audience (User, Developer, Designer, Contributor, etc.): Developer
  • Experience Level (Beginner, Intermediate, Advanced, Any): Intermediate

Prerequisites

It is assumed that the learner has already completed the following lessons:

Learning Objectives

  • Describe when to use an action vs when to use a filter
  • Explain, with examples, how to use the Action and Filter reference to determine the correct hook to use

Related Resources and Other Notes

  • https://developer.wordpress.org/apis/hooks/

Automation Code

//lesson

jonathanbossenger avatar Jul 19 '24 19:07 jonathanbossenger

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

github-actions[bot] avatar Jul 24 '24 14:07 github-actions[bot]

@jonathanbossenger from what I got from this issue, this section is here to teach the learner when to use a filter or an action?

CrochetFeve0251 avatar Aug 21 '24 22:08 CrochetFeve0251

Understand the purpose of an action vs a filter: (Maybe we can talk a bit about the CQRS approach)

  • Action: Change the state without returning a value.
  • Filter: See a value without changing the state

When to use an action:

  • To signal an action will/has been going on to let thirdparties react or adapt to it.

When to use filter:

  • Prevent magic values (Values not named in the code)
  • Prevent imposing your choice to the user while offering a default solution.
  • Hiding complex choice from lambda user not knowing what choice to make.

If you see other scenarios feel free to add them

CrochetFeve0251 avatar Aug 21 '24 22:08 CrochetFeve0251

Hi @CrochetFeve0251, apologies if this wasn't clear. The objective of this lesson would be how to determine which hook or filter to use, in order to achieve a specific goal.

So, for example, if I want to enqueue admin-only scripts, which hook do I use? If I want to process a form submission, which hook should I use?

The general process would be:

  • determine if I need an action or filter hook
  • then review the relevant reference (action or filter) to decide which hook should be used.

The beginner developer lessons on Hooks, Actions and Filters does already cover the basic purpose of the two, but you certainly can expand on that based on what you've shared here already.

Then after that, you can include the process of how to decide which one to use, when developing plugins.

jonathanbossenger avatar Aug 22 '24 08:08 jonathanbossenger

I see this lesson also had the wrong name for some reason, so I've updated it. It also didn't have prerequisite lessons or objectives set, so I've added those as well.

jonathanbossenger avatar Aug 22 '24 08:08 jonathanbossenger

@CrochetFeve0251 I'm checking in to see if you're still happy to continue with scripting the next lesson in this module?

jonathanbossenger avatar Sep 09 '24 14:09 jonathanbossenger

@jonathanbossenger I started to work on it but I didn't push yet the script

CrochetFeve0251 avatar Sep 09 '24 15:09 CrochetFeve0251

Thank you, no pressure from my side, just wanted to make sure the process is still enjoyable. 🙂

jonathanbossenger avatar Sep 09 '24 15:09 jonathanbossenger

So, for example, if I want to enqueue admin-only scripts, which hook do I use? If I want to process a form submission, which hook should I use?

The general process would be:

  • determine if I need an action or filter hook
  • then review the relevant reference (action or filter) to decide which hook should be used. ... Then after that, you can include the process of how to decide which one to use, when developing plugins.

When I think about which hook is best at an advanced level, I also consider (1) what data is made available in the hook's parameters and (2) the execution order of the hook. (eg. filtering a post's data before it is inserted into the database rather than acting on it after creation to then update it is generally more efficient)

These are the two main reasons I often cannot use a particular hook: it either doesn't provide crucial data needed for my custom functionality or it executes too early or late in the execution flow.

Additionally, a nifty trick is you can certainly use a filter hook as an action hook if it meets the necessary criteria. You simply perform your logic and then pass back the filtered value with no modifications to it.

Maybe these are also good concepts to consider for this lession.

MichelleBlanchette avatar Sep 11 '24 12:09 MichelleBlanchette

Logging that a PR for the script has been created. I will review this week.

jonathanbossenger avatar Oct 01 '24 13:10 jonathanbossenger

Script: https://github.com/wptrainingteam/learn-wp-content/blob/main/plugin-developer-learning-pathway/02-Advanced-Hooks/03-correct-the-hook.md

jonathanbossenger avatar Oct 31 '24 10:10 jonathanbossenger

Video for review: https://drive.google.com/file/d/1OtO1GN945fK2MwUwE1MtbvA6Ocn34Foq/view?usp=drive_link

jonathanbossenger avatar Nov 07 '24 19:11 jonathanbossenger

Video for review: https://drive.google.com/file/d/1OtO1GN945fK2MwUwE1MtbvA6Ocn34Foq/view?usp=drive_link

@jonathanbossenger I'm unable to access the video for review.

MichelleBlanchette avatar Nov 09 '24 17:11 MichelleBlanchette

@MichelleBlanchette apologies, this should be public now.

jonathanbossenger avatar Nov 25 '24 09:11 jonathanbossenger

Tutorial/Lessons Review Checklist

Please tick all items you've confirmed:

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

I really enjoyed this lesson. The description and purpose of each mentioned hook was spot on and clearly communicated. I was particularly impressed by the explanation of the plugins_loaded hook and your tips for working with third-party plugins' hooks. 😁 Great work!

MichelleBlanchette avatar Nov 27 '24 02:11 MichelleBlanchette

WordPress.tv: https://wordpress.tv/2024/12/23/determining-the-correct-hook/

jonathanbossenger avatar Dec 23 '24 14:12 jonathanbossenger

00:00 Introduction 00:58 Finding available hooks 01:59 Important hooks to remember 03:12 Using hooks outside of WordPress Core

jonathanbossenger avatar Dec 23 '24 14:12 jonathanbossenger

https://youtu.be/Vy_k100F0Lw

jonathanbossenger avatar Dec 26 '24 19:12 jonathanbossenger

https://learn.wordpress.org/lesson/determining-the-correct-hook/

jonathanbossenger avatar Dec 27 '24 06:12 jonathanbossenger

//publish

jonathanbossenger avatar Dec 27 '24 06:12 jonathanbossenger