Custom hooks.
As a plugin developer, you'll probably try to cover all possible use cases when building your plugins functionality.
However, all websites have unique requirements, and it's impossible to satisfy all of them.
This sometimes means your plugin will end up being really close to what a potential user needs,
but might need one or two minor additions that would make it a perfect solution.
Fortunately, there is a way to allow your plugin's users to extend its functionality without editing the plugin code.
In this lesson, you will learn about custom hooks,
the perfect way to enable your users to customize your plugin to their needs.
Custom hooks are just like the regular hooks that you've already learned about, which are defined by WordPress call.
The only difference is that you are behind the wheel.
You can either create custom action hooks or custom filter hooks through your plugin code
to enable other users or developers the ability to extend the functionality of your plugin.
Custom hooks are a great way to keep focus on your plugin's main functionality.
They make it possible for your plugin user to customize your plugin behavior to meet their needs and implement their own features.
They also allow you to avoid having to worry about making your plugin compatible with every single possible external integration.
As custom hooks give users a way to implement their own custom scenarios,
they will often report their solutions back to you when they find one, which you can share with the rest of your users.
For the rest of the examples in this lesson, download, install and activate version 1.0.2 of the Bookstore plugin
from the training team's GitHub repository on your local WordPress installation.
This plugin is part of a beginner developer learning pathway.
You will often want to create custom actions to trigger before something specific is going to happen or after something specific has happened.
To do this, you call the do action function, passing it the name of the action as a parameter.
Open the Bookstore plugin in your code editor, edit the bookstore.php file and find the Bookstore render booklist function.
This function renders the Bookstore plugin's custom submenu page in the WordPress dashboard.
Let's say you want to make it possible for someone to add their own HTML content to that function.
You can do this by adding action hooks using the do action function.
This way, another developer can hook into the before Bookstore render booklist and after Bookstore render booklist actions
to add their own content to the Bookstore plugin submenu page.
Custom filters are a way to allow someone to change the value of something you define in your code.
To create a custom filter, you call the apply filters function with the name of the filter as the first parameter
and the default value the filter is applied to as the second parameter.
Two good places to add filters to the Bookstore plugin are just before the custom post type and custom taxonomy are registered.
With these filters in place, another developer could hook into either of these filters
and add or remove values from the relevant args array that is used to create the custom post type or custom taxonomy.
In the lesson on working with hooks, you learned that it's possible to set the number of arguments
the callback function can accept from the action or filter hook.
Both the do action and apply filters function signatures accept an unlimited number of optional additional arguments.
These arguments are passed to the callback function when the action or filter is triggered.
When someone hooks a callback function into the action or filter, they can specify how many of these arguments they want to perceive in the callback function.
You can use these arguments to pass optional data to the callback function,
such as the context in which the action or filter is triggered or anything else that might be important.
For example, you could pass the current value of isAdmin as a context argument to the actions in the render function.
Any callback function hooked into the bookstore before renderBooklist or bookstore after renderBooklist action
that is set to receive one argument will now receive the value of isAdmin.
In the lesson on naming collisions, you learned how to avoid naming conflicts in the global PHP namespace.
This is also true when creating custom hooks.
When creating custom hooks, you should always prefix your hook names with a unique identifier.
Ideally, the same one used elsewhere in your plugin.
That way, if someone else creates another hook with a similar name, it won't conflict with yours,
as each one will be prefixed with its own unique identifier.
Under the hood, actions and filters are functionally the same.
The main difference being that actions don't return a value and filters do.
For that reason, you should always use unique names for each hook.
Don't create an action and a filter with the same name.
For example, let's say you want to add an action and a filter to the bookstore registerPostType function.
Doing this will result in conflicts with callback functions registered to the action or the filter,
as it means that the callback will run both when the action and the filter are triggered.
Depending on whether you want to hook a callback into the action or filter, it may cause errors in the code execution.
One way to fix this is to use a more descriptive name for your actions and filters.
You can read more about custom hooks along with some examples
in the WordPress plugin Developer Handbook section on custom hooks.