Covered in this article
What Are Hooks?
Hooks in WordPress allow you to change or add code without editing core files. They are used extensively throughout WordPress and MemberPress and are very useful for developers.
There are two types of hooks: actions and filters.
- Actions allow you to insert custom code wherever the hook is run
- Filters allow you to manipulate and return a variable that it passes to the filter
This documentation is a list of some of the most useful actions and filters in MemberPress.
Using Hooks
If you want to use a hook to add or manipulate code, you can add your custom code in many ways:
- You can add your code at the end of your theme's functions.php file
- Use one of the plugins such as the WPCode plugin (please check this article for details: How to add custom code snippets in WPCode).
Using action hooks
To execute your code using an action hook, you need to use your custom function like this:
add_action( 'action_name', 'your_function_name' ); function your_function_name() { // Your code }
Using filter hooks
To manipulate the passed variable in a filter hook, you need to use your custom function like this:
add_filter( 'filter_name', 'your_function_name' ); function your_function_name( $variable ) { // Your code return $variable; }
Keep in mind that with filters, you must return a value.