Google Adsense Support?
-
Hi, I was trying to use the memberships do hide Adsense adverts to members, but I this plugin is “breaking” the adsense code and it’s not working.
I tried to put the Adsense code directly on the restricted section textbox on the page without success.
I also tried to use the plugin WP Quads to insert the ads on the restricted area but it also does not work.
Is there any support or option to hide Google Ads to membership members?
Thanks.
-
Hello @zekinha,
You shouldn’t use the content restriction boxes to place your ad code.
Ads require a .js file to be loaded for them to work. How did you make this load ? If you have a plugin for these ads I am guessing that this plugin is handling it.
If this script is not loaded, the ads should not show.The idea would be to make that script load only if the currently logged in user is not a member. I can show you some sample code of how that can be achieved.
Let me know.
Regards.
Hi @georgian Cocora,
Thanks for your reply.
I just used a plugin to try to make the ad work (but it didn’t).
Let’s forget about the plugin for the ads. I will try again to explain better to make myself clear.
I have a Google Adsense account, and I wanted to show ads only to NON members, and hide it to members.
The ad code that Google provides is something like this:
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script> <!-- Homepage Leaderboard --> <ins class="adsbygoogle" style="display:inline-block;width:728px;height:90px" data-ad-client="ca-pub-1234567890123456" data-ad-slot="1234567890"></ins> <script> (adsbygoogle = window.adsbygoogle || []).push({}); </script>
Source: https://support.google.com/adsense/answer/181950?hl=en
How can I make this with your members plugin? Is it possible?
Regards,
Hello,
Ok so in your ad tag, the first <script> declaration needs to be added only once, you don’t need to do it for each ad tag that you insert.
What we need to do is load this script conditionally, so it’s only loaded when your users are not members. If we don’t load it, the ads won’t display.
First step is to make sure that you remove that line from every ad tag that you include.
Next, you need to use this piece of code in order to enqueue the ads script:function pmsc_enqueue_ads_script() { if (is_user_logged_in() && function_exists('pms_get_member_subscriptions')) { $user_id = get_current_user_id(); if ($user_id != 0) { $subscription = pms_get_member_subscriptions(array('user_id' => $user_id)); if (!empty($subscription) && (!empty($subscription[0]->expiration_date) && time() <= strtotime($subscription[0]->expiration_date))) return; else wp_enqueue_script('adsense-js', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'); } } wp_enqueue_script('adsense-js', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'); } add_action( 'wp_enqueue_scripts', 'pmsc_enqueue_ads_script' );
This should not include the script when the current user is a member so the ads won’t load.
Try this out and let me know if it works !
Regards.
Hi, Thank you very much for your reply.
I am currently away from my computer, but as soon as I can I will try it and let you know how this went.Regards.
Hi Georgian, I was trying to make this work but I am a bit confused…
So, the Adsense code I have is this:
<script type="text/javascript">// <![CDATA[ google_ad_client = "ca-pub-7012782425415437"; /* MyAdsName */ google_ad_slot = "4420796155"; google_ad_width = 468; google_ad_height = 60; // ]]></script><script src="https://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">// <![CDATA[ // ]]></script>
So, only one <script> tag as you said.
Now, my question is, where do I put my Adsense code and where do I put the code you provided?
Do I put the code on the textarea (when we edit a page or post) or on the “custom messages” area? Or somewhere else?
Thank you.
ZekinhaHey,
The code I provided needs to be placed in a custom plugin or in the
functions.php
file from your theme.From your ad tag, this part can be added only once:
<script src="https://pagead2.googlesyndication.com/pagead/show_ads.js" type="text/javascript">// <![CDATA[ // ]]></script>
So you should enqueue the script from that URL only 1 time and only when users are not members using the code I supplied.
Now, my question is, where do I put my Adsense code and where do I put the code you provided?
You put the ad tags where you want to display an ad, it doesn’t matter.You probably don’t want to use our messages restrictions since then not all users will see it.
Probably the best insertion points are your theme templates.
Regards.
-
This reply was modified 6 years, 11 months ago by
Georgian Cocora.
Hi @georgian Cocora
Thank you for your help and apologies for the very long delay getting back to you. Some stuff happen in my life that I don’t want to publicly share here.
Going back to the problem…
I put this script in my functions.php file inside my theme folder.
function pmsc_enqueue_ads_script() { if (is_user_logged_in() && function_exists('pms_get_member_subscriptions')) { $user_id = get_current_user_id(); if ($user_id != 0) { $subscription = pms_get_member_subscriptions(array('user_id' => $user_id)); if (!empty($subscription) && (!empty($subscription[0]->expiration_date) && time() <= strtotime($subscription[0]->expiration_date))) return; else wp_enqueue_script('adsense-js', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'); } } wp_enqueue_script('adsense-js', '//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'); } add_action( 'wp_enqueue_scripts', 'pmsc_enqueue_ads_script' );
From my very little coding knowledge, this is what I think its happening:
If the user is logged in and the funcion member subscriptions exists, then get the user ID. If the user ID is not 0, check their subscription. If the subscription does not exist (empty) then cancel (return), else load the advertising from Google Adsense.Is this correct?
Then, I created a test adsense code that will place an ad or a pink color so I know it’s working.
That piece of Adsense code I tried to put in a page description, let’s call it “Software”.
Note: the “Display for” option is selected for “Logged in users”, “No ads” plan that I created.
So when I go to the Software page:
Scenario 1: Loading the Software page without bein logged in, does NOT show an ad, just shows the custom message for logged-out users. (should show ad)
Scenario 2: Loading the Software page with a simple user logged in, does NOT show an ad, just shows the custom message for logged-in non-members users. (should show ad)
Scenario 3: Loading the Software page with a user with a “noAds” membership, does SHOW an ad. (should NOT show ad to users with membership)
Any sugestion on what am I doing wrong? Should I put the ad script in other place?
Thanks in advanced.
-
This reply was modified 6 years, 11 months ago by
- The topic ‘Google Adsense Support?’ is closed to new replies.