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 5 replies - 1 through 5 (of 5 total)
  • qoraiche

    (@qoraiche)

    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 ago by qoraiche.
    • This reply was modified 1 year ago by qoraiche.
    • This reply was modified 1 year 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 Grzegorz (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/

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.