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.
Opens WPIDE on
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
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.