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

Determining the correct hook #2702

Open
jonathanbossenger opened this issue Jul 19, 2024 · 13 comments
Open

Determining the correct hook #2702

jonathanbossenger opened this issue Jul 19, 2024 · 13 comments
Assignees

Comments

@jonathanbossenger
Copy link
Collaborator

jonathanbossenger commented Jul 19, 2024

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

Automation Code

//lesson

Copy link
Contributor

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 jonathanbossenger changed the title Determining the correct hook Hook best practices Aug 11, 2024
@CrochetFeve0251
Copy link

@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
Copy link

CrochetFeve0251 commented Aug 21, 2024

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

@jonathanbossenger jonathanbossenger changed the title Hook best practices Determining the correct hook Aug 22, 2024
@WordPress WordPress deleted a comment from github-actions bot Aug 22, 2024
@jonathanbossenger
Copy link
Collaborator Author

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
Copy link
Collaborator Author

jonathanbossenger commented Aug 22, 2024

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.

@WordPress WordPress deleted a comment from github-actions bot Aug 22, 2024
@WordPress WordPress deleted a comment from github-actions bot Aug 22, 2024
@WordPress WordPress deleted a comment from github-actions bot Aug 22, 2024
@jonathanbossenger
Copy link
Collaborator Author

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

@CrochetFeve0251
Copy link

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

@jonathanbossenger
Copy link
Collaborator Author

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

@MichelleBlanchette
Copy link

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.

@jonathanbossenger
Copy link
Collaborator Author

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

@jonathanbossenger
Copy link
Collaborator Author

@jonathanbossenger jonathanbossenger moved this from 👋 Ready to Create to 🔎 Ready for Review in LearnWP Content - Development Nov 7, 2024
@MichelleBlanchette
Copy link

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

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

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

3 participants