Working with Filters

·

·

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:

  1. Appending a banner to post content
  2. Changing excerpt length
  3. 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_namethe_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 svg key → allows SVG uploads
  • Always return the $mimes array 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

FeatureActionFilter
PurposePerform tasksModify or return data
Return valueNot requiredMust return a value
Example hooksinit, wp_footerthe_content, excerpt_length
Typical usageAdd scripts, register CPTsModify content, titles, excerpts, MIME types
Execution flowRuns at a point in WPRuns when data is processed/returned

📌 Best Practices for Filters

  1. Always return data → Never forget to return the modified value.
  2. Use conditional checks → Like is_singular() or is_admin() to avoid unwanted modifications.
  3. Set appropriate priority → Especially if multiple plugins are modifying the same filter.
  4. 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.