What is the difference between actions and filters in WordPress?
Both are hooks, and both are added with a callback function.
| Point | Action | Filter |
|---|---|---|
| Add | add_action() | add_filter() |
| Run | do_action() | apply_filters() |
| Purpose | Do something (print, save, send) | Change a value |
| Return | Nothing | The changed value, always |
add_action( 'tc_before', function () { echo "action: runs, returns nothing\n"; } );
do_action( 'tc_before' );
add_filter( 'tc_title', function ( $title ) { return strtoupper( $title ); } );
echo 'filter: ' . apply_filters( 'tc_title', 'hello' );
Output
action: runs, returns nothing
filter: HELLO
The most common mistake in a filter is to forget return, which makes the value empty. Read Hooks: Actions and Filters.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |