[Resolved] Featured image based on URL parameter

Home Forums Support [Resolved] Featured image based on URL parameter

Home Forums Support Featured image based on URL parameter

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #1299205
    Greg

    Hi guys

    This is a long shot, and goes way beyond the support for GP so totally understand if you’re unable to help – but hoping someone may know of a clever way to setup a filter for this, or having done something similar before.

    I’ve setup URL params to pass some selections to a form on a page, and would love to be able to change the featured image URL for the page based on the URL parameter – do you know any way of achieving this with a snippet so that it changes the featured image URL ahead of the header element pulling in the featured image for the page?

    Thanks for your time
    Greg

    #1299614
    Tom
    Lead Developer
    Lead Developer

    Hey Greg,

    Should be pretty easy:

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        if ( isset( $_GET['your-param'] ) ) {
            $url = esc_url( $_GET['your-param'] );
        }
    
        return $url;
    } );

    esc_url() is important in there for security reasons.

    Let me know πŸ™‚

    #1299615
    Greg

    Thanks for the reply Tom, is it possible to set an image URL based on the query string set in the URL parameter rather than the parameter itself? Sorry, I probably didn’t explain that part very well in my first post.

    So for example, if the URL parameter is set something like:
    /?Product=Product-Name-1
    /?Product=Product-Name-2

    If Product = Product-Name-1 then display image URL /hero-product-image-1.jpg
    If Product = Product-Name-2 then display image URL /hero-product-image-2.jpg

    Thanks
    Greg

    #1299902
    Tom
    Lead Developer
    Lead Developer

    Where would that image URL come from, exactly? Are these pre-existing images in a folder on your site? Or are we getting the featured image of a specific product using its name/ID?

    You can target specific params like this:

    add_filter( 'generate_page_hero_background_image_url', function( $url ) {
        $param = $_GET['your-param'];
    
        if ( isset( $param ) ) {
            if ( 'Product-Name-1' === $param ) {
                $url = 'URL HERE';
            }
    
            if ( 'Product-Name-2' === $param ) {
                $url = 'URL HERE';
            }
        }
    
        return $url;
    } );
    #1300347
    Greg

    Works perfectly, thanks Tom.

    Should I have the esc_url() in there still?

    #1300842
    Tom
    Lead Developer
    Lead Developer

    Not if you’re manually placing the URL. You only want to escape it if you don’t know the result is 100% safe.

    #1301311
    Greg

    I see, thanks again! πŸ™‚

    #1302432
    Tom
    Lead Developer
    Lead Developer

    No problem πŸ™‚

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