- This topic has 9 replies, 3 voices, and was last updated 5 years, 4 months ago by
Tom.
-
AuthorPosts
-
December 13, 2020 at 6:50 am #1578649
Randy
Hi,
I have GP Premium and GB now too. Working on porting over existing website.I have code now working that uses Google Sign In and confirms if the user is a member of my website…it leaves a url parameter, lets call it &noads=true (it takes them off of the page if they are not a confirmed member and removes the url parameter)
So my question…how would I change some basic setup of some pages based on the presence of that url parameter.
For instance, it should
– remove the right sidebar on certain tool pages
– hide ad blocks (not load the ad code at all)
– etc.I’m planning to use GB and make reusable blocks for the tool pages (I think). But I want to make sure I’m using the right combination of sections/generate blocks/elements(hooks) that will work best with GP Premium and GB before I go down a long development hole…
You can get a sense of the pages in the private info block.
Thanks so much for the guidance!December 13, 2020 at 11:24 am #1578919Tom
Lead DeveloperLead DeveloperHi there,
When it comes to the sidebar, you can use this filter: https://docs.generatepress.com/article/generate_sidebar_layout/
For example:
add_filter( 'generate_sidebar_layout', function( $layout ) { if ( isset( $_GET['noads'] ) && $_GET['noads'] ) { // We have the URL parameter - do stuff. return 'no-sidebar'; } return $layout; } );Hiding ad blocks depends on how you’re adding them in the first place.
If they’re Hook Elements, you can use this filter: https://docs.generatepress.com/article/generate_hook_element_display/
This would use the exact same method:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) { if ( 10 === $element_id && isset( $_GET['noads'] ) && $_GET['noads'] ) { $display = false; } return $display; }, 10, 2 );Hope this helps!
December 13, 2020 at 3:20 pm #1579106Randy
Mostly working!
Except I can’t quite get the ad elements to not display.
What is the “, 10, 2 );” at the end of the add_filter? Does that ever need changed?For the ad element, I’m adding it in the generate_after_header hook, but when I echo the $element_id ‘s it the one I’m trying to hide doesn’t get echoed and the chrome console shows that it is still in the document (plus you still see the space for the ad taken up on the page).
Even though right sidebar is being hidden correctly.
Any thoughts on what I’m doing wrong?add_filter( 'generate_sidebar_layout', function( $layout ) { $var = $_GET["myparam"]; if ( $var === "myvalue" && is_page() ) { return 'no-sidebar'; } return $layout; } ); add_filter( 'generate_hook_element_display', function( $display, $element_id ) { $var = $_GET["myparam"]; if ( $element_id === 2480 && $var === "myvalue" && is_page() ) { $display = false; } return $display; }, 10, 2 );December 14, 2020 at 3:44 am #1579635David
StaffCustomer SupportHi there,
i am assuming that members will have a logged in status on your site – if thats the case then you can set a Hook Elements Display Rules >
User: Logged OutThat you could apply to your Advert hook – and it will only display to logged out users.
December 14, 2020 at 7:28 am #1580153Randy
Hi David,
I’m using Google Sign In rather than wordpress to keep track of people who are logged in members, so the way I do that is with a url parameter, let’s call it &myparam=myvalue to stay consistent with the code example above.
The code example works great for hiding the right sidebar, but is not working for hiding the ad div. Any idea why?Thanks again!
December 14, 2020 at 11:09 am #1580487Tom
Lead DeveloperLead DeveloperThat function looks like it should do the trick. You could
var_dump()each variable in the condition to make sure they’re returning what they should be returning.Let me know 🙂
December 14, 2020 at 11:58 am #1580546Randy
Thanks Tom. I tried var_dump() like this:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) { $var = $_GET["myparam"]; var_dump($var, $element_id);…etc
I get this:
string(8) “myvalue” int(2430) string(8) “myvalue” int(2431) string(8) “myvalue” int(2444) string(8) “myvalue” int(2467)Element 2480 is not listed in the var_dump even though that is what is shown when I hover over that ad Element in the Elements tab it shows post.php?post=2480
In that Element settings I am using hook generate_after_header running on Entire site.Am I using the right Element hook for the generate_hook_element_display function?
December 14, 2020 at 7:26 pm #1580845Tom
Lead DeveloperLead DeveloperIf it’s a plain Hook Element, then that’s the correct filter.
What if you increase the
10at the end to something like100?December 15, 2020 at 7:19 am #1581621Randy
Thanks Tom. I tried changing priority like you said to 100, also tried 1, 110 etc. while also changing the Element Hook priority that adds the add block in the first place to 1, 10, 11, 100, 110 etc. Nothing is hiding the ad block.
For testing purposes I took out the url parameter so it should be hiding the ad block on any static page. See url in private info if you can take a quick look?
December 15, 2020 at 12:44 pm #1582024Tom
Lead DeveloperLead DeveloperBack to what you said up here: https://generatepress.com/forums/topic/url-parameter-to-change-layout/#post-1580546
The element ID for that hook isn’t displaying at all?
what if you do this?:
add_filter( 'generate_hook_element_display', function( $display, $element_id ) { var_dump($element_id); return $display; } );It’s not included in the list?
-
AuthorPosts
- You must be logged in to reply to this topic.