# Affiliate Disclosure on Selected Categories

> Shows an affiliate or sponsorship disclosure at the top of posts in the categories you choose, so you never forget to add it by hand.

- Type: PHP
- Runs: Frontend only
- Category: [Content](https://wpide.com/library/category/content/)
- Tags: affiliate, disclosure, compliance
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/affiliate-disclosure-by-category/

## Code

```php
// Category slugs that need the disclosure.
$wpide_disclosure_categories = array( 'reviews', 'deals' );
// Disclosure text (basic HTML allowed).
$wpide_disclosure_text = 'This post contains affiliate links. If you buy through them we may earn a commission, at no extra cost to you.';

add_filter( 'the_content', function ( $content ) use ( $wpide_disclosure_categories, $wpide_disclosure_text ) {
    if ( ! is_singular( 'post' ) || ! in_the_loop() || ! is_main_query() || ! has_category( $wpide_disclosure_categories ) ) {
        return $content;
    }

    return '<p class="wpide-disclosure" style="padding:.75em 1em;font-size:.9em;background:rgba(0,0,0,.04);border-left:3px solid currentColor;">' . wp_kses_post( $wpide_disclosure_text ) . '</p>' . $content;
}, 20 );
```

## Notes

Set the category slugs and the disclosure text at the top of the snippet.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Affiliate Disclosure on Selected Categories" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
