Developing with hooks. As you dive deeper into developing with WordPress hooks,
there are some details about how hooks work that are valuable to understand.
Let's look at some good practices to follow when developing with WordPress hooks.
The first good practice involves naming your hooks.
Let's look at some conventions that WordPress core follows.
Actions are usually added when something changes, so it's advisable for you to use the name of the
action taking place for the hook name. For example, the WordPress core action hook
that's triggered when a post is deleted is simply named deletePost. If you want to add an action
before the actual change, you can prefix it with pre or before. Finally, if you want to add an
action after the actual event, the convention is to use passTense in the action name.
Because filters are generally linked to a variable that can be modified,
filter hooks are often named after the variable that can be modified.
For example, the filter hook that allows you to modify the content of a post is named the content.
When naming your hooks, it's important to avoid naming collisions with hooks from other
plugins or themes. To do this, you should prefix your hook names with your plugin's name or unique
identifier. When hooking a callback to a filter, the type of the variable that's passed from the
filter is not guaranteed, even if it's documented. The reason for this is that the last callback
that runs on the filter determines what is returned by the filter. This might not seem
like an issue at first, because as a developer you will make sure to return a value with the same
type. However, if you recall from the lesson on custom hooks, just as you can hook into actions
and filters, so can other developers. This means that if you add custom hooks to your plugin,
you need to ensure that the data returned is the correct type, otherwise it might break your
plugin's functionality. There are two things to consider when working with filter variable types.
You need to make sure that the value returned to your code from any filter callbacks is the type
you expect. Therefore, you need to validate the data type in your own code. For primitive types
like integer, float or boolean, you can use PHP's typecasting system to ensure the return value is
the correct type. This will automatically cast the return value from any hooked callbacks to the
correct variable type. However, for more complex validation or other types, it is better to
implement a more manual validation check. For a string or an array, it would be better to check
if the return value is the right type rather than using casting, as this could lead to a PHP fatal
error. For example, you can use the PHP is string function to check if the value returned from any
hooked callbacks is a string. If it is not a string, you can either reset the value or throw an error.
If your code is hooking into a filter, you should also validate the type of the variable
passed to your callback function. The fact that the type of the variable returned from a filter
can be modified also means that an incorrectly type variable can be passed to any hooked callback.
Therefore, if you hook a callback function into a filter, it is important to always check the type
of the value you receive before performing any operations on it. You can use the same methods
as before to validate the type of the variable you receive. For more information on PHP's type system,
you can refer to the PHP documentation on types and more specifically the section on type juggling.
There's also a section dedicated to the various variable handling functions for checking a
variable's type. Another good thing to know about developing with hooks is how to get information
about them. WordPress allows a callback function or method to be used on more than one hook.
Due to this, it can sometimes be unclear on which hook the callback is currently running.
In any callback function, it is possible to use the current filter
or current action functions to determine the current filter or action the callback is running on.
Sometimes it is important to know if a hooked callback has already been run,
possibly to prevent it from running again. Again, inside any callback function, you can use the
did filter function to check how many times a filter has been applied during the current request
and the did action function to check how many times the action has been applied.
The WordPress documentation has a dedicated section on hooks in the Common API's handbook,
which is recommended reading when developing with hooks.