Hi, the API Usage of addFilter hook:
addFilter( 'hookName', 'namespace', 'functionName', callback, priority );
More Informations:
packages-hooks/#api-usage
-
This reply was modified 7 years, 4 months ago by
qoraiche.
-
This reply was modified 7 years, 4 months ago by
qoraiche.
-
This reply was modified 7 years, 4 months ago by
qoraiche.
Hi, thank you, I am aware of the docs page, but it does not answer my question. What is the purpose of the namespace argument? How can I use it in a valuable or interesting way?
Hi there,
did you ever find out or get an answer? I also have no idea. The examples I found were not very helpful…
The purpose of a namespace is to avoid name collisions, so eg. you and I can each define a removeCustomClass() callback function and neither one overwrites the other. The slug of your plugin/theme is probably an appropriate choice if nothing else seems obviously better (eg. company name for reusable components, or …).
It works the same as in PHP where you use the name of the function so you could have a reference to remove this filter later:
wp.hooks.removeFilter(
'blocks.registerBlockType',
'what_is_this_line'
);
If you would omit the 2nd param, then all filters registered under blocks.registerBlockType would get removed.
https://developer.ww.wp.xz.cn/reference/functions/remove_filter/
This is definitely not clearly answered by the docs. The most clear documentation I found was in the source code:
addFilter<any>(hookName: string, namespace: string, callback: (firstArg: any, ...rest: any[]) => any, priority?: number): void
Adds the hook to the appropriate hooks container.
@param hookName — Name of hook to add.
@param namespace — The unique namespace identifying the callback in the form vendor/plugin/function.
@param callback — Function to call when the hook is run.
@param priority — Priority of this hook.
Why it’s supposed to be in the form vendor/plugin/function is an unanswered question, but in agreement with @jnorell’s comment, it seems that it is a safe assumption that any string that’s not used by another plugin will work. But I guess it also has a double function as a unique identifier of the hook for the purpose of removal, but that feature doesn’t appear to be documented at all. In that case, you’d want a string that’s not only unique to your plugin, but also unique within your plugin. That answers why it should contain the plugin/function portions, but not what the vendor portion is.