Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

Code

// 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 add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.