Skip to content

How to use hooks with the Better Plugin Boilerplate

Beda Schmid edited this page Nov 15, 2021 · 2 revisions

Add Filters or Actions

To hook into the flow of code using add_filter and add_action when working with the Better Plugin Boilerplate, of course you can use use the standard add_filter or add_action syntax. However since everything is Object Oriented Code, you will have to pass an array with Class and Method as second argument to the hooks, instead of just a function to add.

To make that easier, the Plugin provides a class, which handles this for you, and thus you can use always the same and easy to remember syntax, passing the Object and Method separately instead of an array of Class and Method when adding a hook.


Remove Filters or Actions

To remove filters or actions added with the plugins specific way, you can not use the WordPress or ClassicPress "classic" approach, but must use the remove_action and remove_filter of the Plugin_Name_Loader Class.


Examples

The conventional way in WordPress or ClassicPress OOP code

  1. Inside a Class add_filter( 'the_hook', array( $this, 'the_method') );
  2. Outside a Class add_filter( 'the_hook', array( new The_Class(), 'slide' ) );

With the Better Plugin Boilerplate

  1. Inside a Class add_filter( 'the_hook', $this, 'method' ); (or self)
  2. Outside a Class add_filter( 'the_hook', $object, 'method' ); (where $object describes an instance of The_Class())

Remove filters and actions added with the Plugin 3. Remove an action with remove_action( 'the_hook', 'The_Class', 'the_method' ); 4. Remove a filter with remove_filter( 'the_hook', 'The_Class', 'the_method' );


ℹ️ Both the plugins add_ and remove_ (filter and action) methods are members of the Plugin_Name_Loader Class, so you need to instantiate that class first to use them.