What is the difference between actions and filters in WordPress?

Both are hooks, and both are added with a callback function.

PointActionFilter
Addadd_action()add_filter()
Rundo_action()apply_filters()
PurposeDo something (print, save, send)Change a value
ReturnNothingThe 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.