Filters are a powerful part of WordPress hooks that let you modify data before it’s displayed or processed. Unlike actions, filters must return a value, which WordPress then uses.
In this guide, we’ll explore three common and practical uses of filters:
- Appending a banner to post content
- Changing excerpt length
- Allowing SVG uploads
📌 1. Appending a Banner to Content
Sometimes you want to add custom content, like a promotion or call-to-action, automatically at the end of posts. You can achieve this using the the_content filter.
🔹 Syntax
add_filter( 'hook_name', 'callback_function', $priority = 10, $accepted_args = 1 );
- hook_name →
the_content - callback_function → your function to modify post content
- priority → optional, default
10 - accepted_args → number of arguments your function accepts
🔹 Example
add_filter( 'the_content', function ( $content ) {
if ( is_singular( 'post' ) ) {
$banner = '<p class="promo">Enjoying this post? Subscribe!</p>';
return $content . $banner;
}
return $content;
});
Explanation:
- Checks if the content is a single post using
is_singular('post'). - Appends a promo banner HTML to the post content.
- Returns the modified content to WordPress.
✅ Tip: Always return the content; otherwise, you risk breaking post output.
📌 2. Changing Excerpt Length
WordPress excerpts are short summaries of posts. By default, they are 55 words long. You can change this using the excerpt_length filter.
🔹 Example
add_filter( 'excerpt_length', function ( $length ) {
return 20;
}, 999 );
Explanation:
$length→ the current number of words in the excerpt.return 20;→ sets all excerpts to 20 words.999→ priority, ensures this filter runs late so it overrides other modifications.
✅ Tip: Use a high priority if other plugins or themes also modify excerpts.
📌 3. Allowing SVG Uploads
By default, WordPress does not allow SVG files for security reasons. You can safely enable them by modifying the allowed MIME types using the upload_mimes filter.
🔹 Example
add_filter( 'upload_mimes', function ( $mimes ) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
Explanation:
$mimes→ associative array of allowed MIME types- Adding
svgkey → allows SVG uploads - Always return the
$mimesarray to maintain other allowed file types
✅ Security Tip: Allowing SVGs can be risky. Consider sanitizing SVG files using a plugin like Safe SVG.
📌 Key Differences Between Filters and Actions
| Feature | Action | Filter |
|---|---|---|
| Purpose | Perform tasks | Modify or return data |
| Return value | Not required | Must return a value |
| Example hooks | init, wp_footer | the_content, excerpt_length |
| Typical usage | Add scripts, register CPTs | Modify content, titles, excerpts, MIME types |
| Execution flow | Runs at a point in WP | Runs when data is processed/returned |
📌 Best Practices for Filters
- Always return data → Never forget to return the modified value.
- Use conditional checks → Like
is_singular()oris_admin()to avoid unwanted modifications. - Set appropriate priority → Especially if multiple plugins are modifying the same filter.
- Keep it secure → Avoid inserting unsafe HTML or allowing dangerous file types without sanitization.
✅ Summary
Filters in WordPress allow you to modify content, excerpts, MIME types, and other data before it’s displayed.
- Append Banner: Add custom messages or CTAs to posts.
- Change Excerpt Length: Control how many words appear in summaries.
- Allow SVG Uploads: Extend supported file types safely.
Mastering filters lets you customize WordPress behavior without altering core files, making your site more flexible, maintainable, and dynamic.
