Skip to content

Determining the correct hook #2702

Closed
Closed

Description

@jonathanbossenger

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

Activity

  1. github-actions commented on Jul 24, 2024

    @github-actions
    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
  2. changed the title [-]Determining the correct hook[/-] [+]Hook best practices[/+] on Aug 11, 2024
  3. CrochetFeve0251 commented on Aug 21, 2024

    @CrochetFeve0251

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

  4. CrochetFeve0251 commented on Aug 21, 2024

    @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

  5. changed the title [-]Hook best practices[/-] [+]Determining the correct hook[/+] on Aug 22, 2024
  6. jonathanbossenger commented on Aug 22, 2024

    @jonathanbossenger
    CollaboratorAuthor

    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.

  7. jonathanbossenger commented on Aug 22, 2024

    @jonathanbossenger
    CollaboratorAuthor

    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.

  8. jonathanbossenger commented on Sep 9, 2024

    @jonathanbossenger
    CollaboratorAuthor

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

  9. CrochetFeve0251 commented on Sep 9, 2024

    @CrochetFeve0251

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

  10. jonathanbossenger commented on Sep 9, 2024

    @jonathanbossenger
    CollaboratorAuthor

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

  11. MichelleBlanchette commented on Sep 11, 2024

    @MichelleBlanchette

    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.

  12. 5 remaining items

  13. jonathanbossenger commented on Nov 7, 2024

    @jonathanbossenger
    CollaboratorAuthor
  14. moved this from 👋 Ready to Create to 🔎 Ready for Review in LearnWP Content - Developmenton Nov 7, 2024
  15. MichelleBlanchette commented on Nov 9, 2024

    @MichelleBlanchette
  16. jonathanbossenger commented on Nov 25, 2024

    @jonathanbossenger
    CollaboratorAuthor

    @MichelleBlanchette apologies, this should be public now.

  17. MichelleBlanchette commented on Nov 27, 2024

    @MichelleBlanchette

    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).

    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!

  18. moved this from 🔎 Ready for Review to ✅ Preparing to Publish in LearnWP Content - Developmenton Dec 9, 2024
  19. jonathanbossenger commented on Dec 23, 2024

    @jonathanbossenger
    CollaboratorAuthor
  20. jonathanbossenger commented on Dec 23, 2024

    @jonathanbossenger
    CollaboratorAuthor

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

  21. jonathanbossenger commented on Dec 26, 2024

    @jonathanbossenger
    CollaboratorAuthor
  22. jonathanbossenger commented on Dec 27, 2024

    @jonathanbossenger
    CollaboratorAuthor
  23. jonathanbossenger commented on Dec 27, 2024

    @jonathanbossenger
    CollaboratorAuthor

    //publish

  24. moved this from ✅ Preparing to Publish to 📜 Published or Closed in LearnWP Content - Developmenton Dec 27, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

[Content] PublishedMarks closed issues as content that was actually published.

Type

No type

Projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions