site-kit-wp
site-kit-wp copied to clipboard
Add filter for more finegrained disabling of auto ads
Feature Description
The controls added in response to https://github.com/google/site-kit-wp/issues/2681 offer a quick and easy way to disable ads for logged in users and probably fulfill the needs of the vast majority of users.
However, some more fine grained control would be useful in some cases.
For example I'm working on a site where I want to be able to disable ads for some logged in users, but not others.
A quick solution would be to amend the final return statement of the can_activate function in Auto_Ad_Guard from return true; to return apply_filters( 'googlesitekit_autoads_can_activate', true ); or similar.
That would make it possible to apply more control to when ads are disabled with no additional clutter in the user interface.
Do not alter or remove anything below. The following sections will be managed by moderators only.
Acceptance criteria
Implementation Brief
Test Coverage
QA Brief
Changelog entry
Hi there! You can disable tags for any module in Site Kit using you're own logic by hooking into the googlesitekit_{module}_tag_blocked filter (where module is one of analytics, adsense, etc). There is also a variant of this filter for AMP (googlesitekit_{module}_tag_amp_blocked), so if you use AMP you'd want to use both of these. Returning false for either will prevent the tag from being output.
Does that work for your needs?
@funnelblockspro Let me know if you need any assistance with the googlesitekit_{module}_tag_blocked filter.
The below would apply to ensure Site Kit doesn't place your AdSense snippet for logged in users who have an editor role.
add_filter( 'googlesitekit_adsense_tag_blocked' , 'restrict_adsense_snippet' );
function restrict_adsense_snippet() {
// restrict ads for editors programmatically
if ( current_user_can('editor'))
return '__return_true';
}
The same can be applied using the googlesitekit_adsense_tag_amp_blocked filter.
Closing this with the googlesitekit_adsense_tag_blocked available to ensure the AdSense snippet is not inserted for specific roles. The same filter can be used with modification to ensure Site Kit doesn't insert any AdSense code on content types, on specific pages, categories or custom post types.
There is a setting within Site Kit to ensure ads don't appear for logged in users. This is enabled by default (https://github.com/google/site-kit-wp/issues/2681)