I'm just looking for some quick help if possible. I've just enabled Jetpack ads on my site, was hoping to use ad-free viewing as a Patreon reward and found the posts on hooks and conditional tags. Since the option is missing from Jetpack's own settings, I'm assuming that pasting the hooks/tags into functions.php is the best way to do this, but I'm a bit of a novice when it comes to coding – especially PHP – and would just like to confirm that before I go making any modifications.
I'm guessing that I would use something along the lines of the following (and while I'm at it, disabling ads in the excerpt in general because they ruin my theme, haha):
if ( is_user_logged_in(true) {
add_filter( 'wordads_excerpt_disable', '__return_true' );
add_filter( 'wordads_content_disable', '__return_true' );
add_filter( 'wordads_inpost_disable', '__return_true' );
add_filter( 'wordads_header_disable', '__return_true' );
}
But I'm not 100% certain. Can anyone confirm, deny, and/or edit the coding so that it's right? I'm also assuming that I can just paste it anywhere in the file, as has been the case with other database modifications I've seen?
Thanks so much in advance! :)
I'm just looking for some quick help if possible. I've just enabled Jetpack ads on my site, was hoping to use ad-free viewing as a Patreon reward and found the posts on hooks and conditional tags. Since the option is missing from Jetpack's own settings, I'm assuming that pasting the hooks/tags into functions.php is the best way to do this, but I'm a bit of a novice when it comes to coding – especially PHP – and would just like to confirm that before I go making any modifications.
I'm guessing that I would use something along the lines of the following (and while I'm at it, disabling ads in the excerpt in general because they ruin my theme, haha):
if ( is_user_logged_in(true) {
add_filter( 'wordads_excerpt_disable', '__return_true' );
add_filter( 'wordads_content_disable', '__return_true' );
add_filter( 'wordads_inpost_disable', '__return_true' );
add_filter( 'wordads_header_disable', '__return_true' );
}
But I'm not 100% certain. Can anyone confirm, deny, and/or edit the coding so that it's right? I'm also assuming that I can just paste it anywhere in the file, as has been the case with other database modifications I've seen?
Thanks so much in advance! :)
Share Improve this question edited Jun 8, 2019 at 11:04 fuxia♦ 107k39 gold badges255 silver badges459 bronze badges asked Jun 8, 2019 at 7:58 JakeJake 11 Answer
Reset to default 1Your code should be the good one. You just have to use is_user_logged_in()
into a hook instead of calling it directly, and probably to early.
function wp_se_339916() {
if ( is_user_logged_in() {
add_filter( 'wordads_excerpt_disable', '__return_true' );
add_filter( 'wordads_content_disable', '__return_true' );
add_filter( 'wordads_inpost_disable', '__return_true' );
add_filter( 'wordads_header_disable', '__return_true' );
}
}
add_action( 'template_redirect', 'wp_se_339916' );
Using the template_redirect
hook could be a good solution. You'll just have to paste this into your functions.php
file. https://developer.wordpress/reference/functions/is_user_logged_in/