-
Notifications
You must be signed in to change notification settings - Fork 10
How to use hooks with the Better Plugin Boilerplate
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.
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.
The conventional way in WordPress or ClassicPress OOP code
- Inside a Class
add_filter( 'the_hook', array( $this, 'the_method') );
- Outside a Class
add_filter( 'the_hook', array( new The_Class(), 'slide' ) );
With the Better Plugin Boilerplate
- Inside a Class
add_filter( 'the_hook', $this, 'method' );
(orself
) - Outside a Class
add_filter( 'the_hook', $object, 'method' );
(where$object
describes an instance ofThe_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.
- Purpose of this Boilerplate
- The File Structure
- The Code Architecture
- How to use hooks with the Better Plugin Boilerplate
- How to customise and personalise the Plugin code
- Classes and Functions included in the Plugin code