Site logo

[Support request] url parameter to change layout

Home Forums Support [Support request] url parameter to change layout

Home Forums Support url parameter to change layout

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #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!

    #1578919
    Tom
    Lead Developer
    Lead Developer

    Hi 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!

    #1579106
    Randy

    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 );
    #1579635
    David
    Staff
    Customer Support

    Hi 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 Out

    That you could apply to your Advert hook – and it will only display to logged out users.

    #1580153
    Randy

    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!

    #1580487
    Tom
    Lead Developer
    Lead Developer

    That 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 🙂

    #1580546
    Randy

    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?

    #1580845
    Tom
    Lead Developer
    Lead Developer

    If it’s a plain Hook Element, then that’s the correct filter.

    What if you increase the 10 at the end to something like 100?

    #1581621
    Randy

    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?

    #1582024
    Tom
    Lead Developer
    Lead Developer

    Back 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?

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.