🎯 By the end of this lesson you can…
- Modify values with add_filter
- Chain several filters
- Create filterable values in your own plugin
🔌 WordPress PHP
filters.php
👆 Tap a line to explain it
1<?php2add_filter( 'the_title', function ( $title ) {3 return '⭐ ' . $title;4} );5add_filter( 'the_title', 'strtoupper', 20 );6 7the_post();8echo get_the_title(), "\n";9 10$price = apply_filters( 'shop_price', 1000, 'VIP' );11add_filter( 'shop_price', fn( $p, $member ) => $member === 'VIP' ? $p * 0.8 : $p, 10, 2 );12echo apply_filters( 'shop_price', 1000, 'VIP' ), "\n";
🛠️ Reading time filter
Add a filter to the_content that appends (⏱️ N min read) to the content, where N = ceil(word count ÷ 200) and words are counted from the text without tags. Then print apply_filters( 'the_content', $text ) for the input text.
📌 Key takeaways
- A filter callback must return the (changed) value.
apply_filters( 'hook', $value, ...$extra )runs every callback in order.- Offer filters in your plugin so others can customise it without editing your code.
Finished reading & practising?
Saved in this browser. Create a free account to keep it forever.