Support » Plugin: Gutenberg » wp.hooks.addFilter syntax?

  • Resolved Scott Fennell

    (@scofennellgmailcom)


    The goal is to remove the Advanced -> Additional CSS Class from the block editor. I have this working, but can someone please explain my line 12, ‘what_is_this_line’:

    `
    function lxbDeRemoveCustomClassName( settings, name ) {

    return lodash.assign( {}, settings, {
    supports: lodash.assign( {}, settings.supports, {
    customClassName: false
    } ),
    } );
    }

    wp.hooks.addFilter(
    ‘blocks.registerBlockType’,
    ‘what_is_this_line’,
    lxbDeRemoveCustomClassName
    );
    `

Viewing 6 replies - 1 through 6 (of 6 total)
  • Hi, the API Usage of addFilter hook:

    addFilter( 'hookName', 'namespace', 'functionName', callback, priority );

    More Informations:

    packages-hooks/#api-usage

    • This reply was modified 1 year, 10 months ago by qoraiche.
    • This reply was modified 1 year, 10 months ago by qoraiche.
    • This reply was modified 1 year, 10 months ago by qoraiche.
    Scott Fennell

    (@scofennellgmailcom)

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

    Plugin Author Greg Ziółkowski

    (@gziolo)

    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.wordpress.org/reference/functions/remove_filter/

    Jo Sprague

    (@josiahsprague)

    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.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘wp.hooks.addFilter syntax?’ is closed to new replies.